Announcing: A Free Book, A New Course, A Huge Price Cut...
It's a massive ship day. We're launching a free TypeScript book, new course, giveaway, price cut, and sale.
In pretty much any frontend application, you'll likely have encountered this error:
Property 'X' does not exist on type 'Window & typeof globalThis'.
window .X ;Property 'X' does not exist on type 'Window & typeof globalThis'.2339Property 'X' does not exist on type 'Window & typeof globalThis'.
In this article, we'll lay out several different solutions to this issue.
The Window
interface is defined globally in a file called lib.dom.d.ts
. You can change it using various techniques:
.ts
or .tsx
file, you can use declare global
and interface Window
:declare global {
interface Window {
X : number;
}
}
window .X ;
.d.ts
file, you can just specify interface Window
.interface Window {
X: number;
}
declare const window
in a .ts
or .tsx
file:declare const window : {
X : number;
} & Window ;
window .X ;
The interface Window
lives in the global scope in TypeScript. It ships as part of the DOM types in lib.dom.d.ts
, which describes what methods are available in the browser.
The issue comes up when a third-party script (or perhaps our own code) adds something to the window:
window .X = Date .now ();
This is a problem because TypeScript doesn't know about the X
property, and so it throws an error when you try to access it:
window .X ;Property 'X' does not exist on type 'Window & typeof globalThis'.2339Property 'X' does not exist on type 'Window & typeof globalThis'.
So, we need to somehow change the global definition of Window
to include the new property that TypeScript doesn't know about.
declare global
in a .ts
or .tsx
fileThe first solution is to use declare global
to change the global definition of Window
:
declare global {
interface Window {
X : number;
}
}
window .X ;
This works for two reasons. First, declare global
tells TypeScript that anything inside it should be added to the global scope.
Second, we take advantage of declaration merging. This is a rule in TypeScript where interfaces with the same name in the same scope get merged. So by redeclaring Window
in the same scope, we can append new properties to it.
This wouldn't work if we used type
because types don't support declaration merging.
declare global {
type Window = {Duplicate identifier 'Window'.2300Duplicate identifier 'Window'. X : number;
};
}
This solution only works inside a .ts
or .tsx
file because declare global
only works inside a module. So this solution is a little awkward in terms of where you place the definition in your project. In its own module? Colocated with something else?
.d.ts
fileA neater solution is to use interface Window
in a .d.ts
file:
// window.d.ts (choose any filename you like)
interface Window {
X: number;
}
// your-app.ts
window .X ;
This works because anything you place in a .d.ts
automatically goes into the global scope, so there's no need for declare global
.
It also lets you place the global definition in a single file, on its own, which feels a little cleaner than trying to figure out which .ts
file to put it in.
If you're concerned about adding types into the global scope, you can use declare const window
to override the type of window
in a single module:
declare const window : {
X : number;
} & Window ;
window .X ;
declare const window
acts like const window
, but on the type level. So it only acts on the current scope — in this case, the module. We declare the type as the current Window
, plus the new property X
.
This solution gets a little annoying if you need to access window.X
in multiple files because you'll need to add the declare const window
line to each file.
Personally, I tend to reach for solution 2 — a .d.ts
file. It's the fewest lines of code and the easiest to place in your project.
I don't mind adding types into the global scope. By actually changing the type of Window
, you're more accurately describing the environment your code executes in. In my book, that's a bonus.
But if you're really concerned about it, use the declare const
solution.
Share this article with your friends
It's a massive ship day. We're launching a free TypeScript book, new course, giveaway, price cut, and sale.
Learn why the order you specify object properties in TypeScript matters and how it can affect type inference in your functions.
Learn how to use corepack
to configure package managers in Node.js projects, ensuring you always use the correct one.
Learn how to strongly type process.env in TypeScript by either augmenting global type or validating it at runtime with t3-env.
Discover when it's appropriate to use TypeScript's any
type despite its risks. Learn about legitimate cases where any
is necessary.
Learn why TypeScript's types don't exist at runtime. Discover how TypeScript compiles down to JavaScript and how it differs from other strongly-typed languages.