The article shows how to solve the error by extending the Window or globalThis interface.
# Property does not exist on type Window in TypeScript
The "Property does not exist on type 'Window & typeof globalThis'" error occurs when we access a property that doesn't exist on the Window interface.
To solve the error, extend the Window interface in a .d.ts file and add the property you intend to access on the window object.
Here is an example of how the error occurs:
index.ts
// ⛔️ Property 'example' does not exist on// type 'Window & typeof globalThis'.ts(2339)window.example='bobbyhadz.com';console.log(window.example);
property does not exist on type window in typescript
In your src directory, create a types directory that contains the following index.d.ts file:
src/types/index.d.ts
The code for this article is available on GitHubThe code sample shows how to extend the Window interface with a property named example that has a type of string. This will be different in your case, so make sure to adjust the property names and the types.
The provided file simply adds an example property with a type of string, which is most likely not what you need.
TypeScript looks for .d.ts files in the same places it looks for your regular .ts files. The location is determined by the include and exclude settings in yourtsconfig.json file.
TypeScript will merge the declared from you Windowinterface with the original Window interface, so when you use the window object, you will be able to access properties from both interfaces.
The problem is with ts-node not recognizing local declaration files.
To resolve this, use the --files flag with your ts-node command, so instead of ts-node ./src/index.ts you should run ts-node --files ./src/index.ts.
I use nodemon with ts-node and here are the contents of my nodemon.json file.
nodemon.json
After adding the --files flag (only required if using ts-node), restart your server and you should be good to go.
Note that this makes the sum function and the example property accessible directly (globally) and on the global object.
index.ts
# Add the path to your types directory to tsconfig.json
If you still get an error in your IDE, try adding the path to your types directory to your tsconfig.json file.
tsconfig.json
We used the export {} line in our index.d.ts file to mark it as an external module. A module is a file that contains at least 1 import or export statement. We are required to do that to be able to augment the global scope.
Note that you will have to change the contents of the provided index.d.ts file according to your use case.
You should add the names (and types) of all of the properties you intend to access on the global object.
src/types/index.d.ts
The provided file simply adds an example property with a type of any and a sum method, which is most likely not what you need.
TypeScript looks for .d.ts files in the same places it looks for your regular .ts files.
The location is determined by the include and exclude settings in your tsconfig.json file.
TypeScript will merge the typings you declared on the global object with the original typings, so you will be able to access properties and methods from both declarations.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.You can use the search field on my Home Page to filter through all of my articles.