Ambient declarations on Declaration files page has erroneous code example https://basarat.gitbook.io/typescript/type-system/intro/d.ts

Author: AnupamKhoslaCreated Jun 20, 2025Updated Jun 20, 2025

Tutorial Documentation: https://basarat.gitbook.io/typescript/type-system/intro/d.ts

You can tell TypeScript that you are trying to describe code that exists elsewhere (e.g. written in JavaScript/CoffeeScript/The runtime environment like the browser or Node.js) using the declare keyword. As a quick example:

foo = 123; // Error: foo is not defined vs.

declare var foo: any; foo = 123; // allowed

Here foo = 123; is not allowed in javascript at all, because of the reference error. In Typescript, you can't have foo = 123 in the declaration file .d.ts; it is supposed to have only type declarations.

foo = 123; in a .ts file along with declare var foo: any; in the .d.ts file will cause Typescript to skip the reference error.