Does this error look familiar? Property ‘AddressFinder’ does not exist on type ‘Window & typeof globalThis’.ts(2339)
There will come a time in TypeScript when you are not dealing with package-managed dependencies from Npm. These will come in the form of scripts added into the head or footer of your application. Furthermore, these global scripts might not have typings.
You will also run into this error for numerous jQuery plugins and other libraries which work on the premise of modifying the global Window object.
In my instance, I was using the AddressFinder API which uses a provided script you embed into your application.
Adding in your own custom typings is a last resort. You should always check to see if there are officially provided typings for your package/script before proceeding to add in your own.
In older versions of TypeScript you could just throw in an interface called Window
and it would automatically add in your typing values to the Window object — times have changed and now the Window
interface lives under the global scope instead.
declare global {
interface Window {
AddressFinder: any;
}
}
By specifying declare global
you’re saying whatever interfaces you specifying inside will be part of the global scope. Simply create a Window
interface and then declare your properties inside. I am being naughty and using any
to get myself out of a pickle, but will sort out proper typings later.