🔷 TypeScript Intermediate

What are ambient declarations in TypeScript?

Why Interviewers Ask This

Mid-level TypeScript roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

Ambient declarations tell the TypeScript compiler about the shape of JavaScript code that exists at runtime but was not written in TypeScript. They use the declare keyword without providing an implementation. declare var __VERSION__: string; — tells TS that a global variable exists. declare function require(module: string): any;. declare class Animal { name: string; }. declare module "lodash" { export function chunk<T>(array: T[], size: number): T[][]; }. Ambient declarations are placed in .d.ts declaration files (type definition files). They have no runtime equivalent — the compiler uses them for type checking but emits nothing. The @types organization on npm publishes community-maintained declaration files for popular JavaScript libraries (@types/react, @types/node, etc.). declare global { interface Window { myProp: string; } } augments the global scope. The typeRoots and types tsconfig options control which type declarations are included.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last TypeScript project, I used this when...' immediately makes your answer more credible and memorable.