The previous answer looks correct for fixing type recognition, but I think the question is about accessing the Navigator read-only property. What's needed is the missing window reference like this:
const connection = window.navigator.connection