patch navigator interface in TypeScript
Problem
There is a experiment api in Navigator called deviceMemory, which we want to use in our App, but since it’s experimental, it’s not in the definition file yet. So if you call navigator.deviceMemory , the TypeScript compiler will yell at you.
Solution
In order to fix this, you can simply add a navigator.d.ts in your folder , and put the declaration in there. And it will pass the compiler.
Note, It doesn’t have to be a navigator.d.ts file, you can put in other type definition file, but don’t put the interface Navigator part under declare global , because it won’t merge with existing Navigator declaration.
//wrong way
declare namespace global {
interface Navigator {
deviceMemory: number
}
}
// correct way - top level
interface Navigator {
deviceMemory: number
}
Ref
https://stackoverflow.com/questions/40382056/typescript-navigator-merging-with-lib-d-ts