Declaration Files
What
Declaration files (ending in .d.ts) are “type-only” files. They contain no executable code; instead, they describe the shape of existing JavaScript code to the TypeScript compiler.
How
You use the declare keyword to define the structure of global variables or external modules:
// global.d.ts
declare global {
interface Window {
myLegacyApp: { version: string };
}
}
Why
When you use a library or a global variable that was written in vanilla JavaScript, TypeScript has no way of knowing what properties or methods it has. Declaration files bridge this gap, giving you full autocomplete and type hints for code that isn’t actually written in TypeScript.
Summary
.d.ts files allow TypeScript to “understand” and provide safety for any JavaScript code in your project.