// .d.ts files provide type info without runtime code
Note Declaration files describe the shape of existing JavaScript code. They contain only type information - no executable code. TypeScript automatically picks up .d.ts files in your project. Most npm packages include their own or have a @types/package companion.
// Tell TypeScript about a global that exists at runtimedeclareconst gtag:(
command:"config"|"event",
targetId: string,
params?:Record<string, unknown>)=>void;// Tell TypeScript about a global classdeclareclassStripe{constructor(apiKey: string);
charges:{create(params:{ amount: number; currency: string }):Promise<unknown>;};}gtag("event","purchase",{ value:99.99});
Output
// declare tells TypeScript 'trust me, this exists at runtime'
Note declare is for things that already exist in the runtime environment but TypeScript cannot see (global scripts, CDN libraries, injected variables). It emits zero JavaScript. Using declare when the value does not actually exist leads to runtime crashes with no compile-time warning.
Frequently asked questions
How does TypeScript handle ambient declaration?
TypeScript covers this with 2 copy-ready snippets on this page. The "Declaration Files (.d.ts)" snippet in TypeScript uses `// filename.d.ts`.
Which code does the TypeScript example use?
The "Declaration Files (.d.ts)" snippet uses `// filename.d.ts`, from the Modules & Declaration Files section of the TypeScript cheat sheet.
What other TypeScript snippets are shown for "ambient declaration"?
Besides "Declaration Files (.d.ts)", this page also shows "declare Keyword".
Is there anything to watch out for?
Yes. For "Declaration Files (.d.ts)": Declaration files describe the shape of existing JavaScript code. They contain only type information - no executable code. TypeScript automatically picks up .d.ts files in your project. Most npm packages include their own or have a @types/package companion.