Home > Back-end >  How to type vendor specific property in Typescript?
How to type vendor specific property in Typescript?

Time:11-12

How can I type this connection type here? Is there any helper type? I am not able to find an answer in lib.dom.d.ts

    const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;

The error I am getting

Error: Property 'mozConnection' does not exist on type 'Navigator'.
export const useConnection = (): void => {
        const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;


Error: Property 'webkitConnection' does not exist on type 'Navigator'.
export const useConnection = (): void => {
        const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection

CodePudding user response:

You can extend Navigator interface. It will be merged with the lib.dom declaration. Playground

interface Navigator {
    mozConnection?: Navigator["connection"]
    webkitConnection?: Navigator["connection"]
}

 const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
  • Related