Example: Add a Property to the Window type in TypeScript
// <any-name>.d.ts
export declare global {
interface Window {
someProperty: SomeType;
}
}
Explanation
// <any-name>.d.ts
Declaration files (files ending with .d.ts
) are used to declare types for code unavailable to the TypeScript compiler.
export declare global {
// ...
}
Declaration files use ES Modules syntax. ES Modules require at least one export statement; therefore global
is exported. Alternatively, { }
(nothing) could be explicitly exported to satisfy the compiler. This is only necessary if there are no other exports.
The Window
interface is global, therefore changes to that interface need to be within the global
scope.
declare global {
interface Window {
someProperty: SomeType;
}
}
The Window
interface is extended through declaration merging.
Broader Topics Related to TypeScript: Extend the Window (globalThis) Object

JavaScript and TypeScript recipes
Quick and easy to copy recipes for JavaScript and TypeScript

Web Development
Tools and techniques for building websites and web-applications

TypeScript
TypeScript: A programming language that adds optional static typing to JavaScript