Declaration files and ambient types
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Write a .d.ts that describes a JavaScript module.
- Distinguish declare from an implementation.
- Use ambient declarations for globals and modules.
Overview
#What a .d.ts is
A declaration file contains only types. It tells the checker about code that already exists in JavaScript.
declare function parse(input: string): unknown;
declare const VERSION: string;
Describing a module
declare module "legacy-lib" {
export function load(id: string): Promise<unknown>;
export const version: string;
}
declare global
declare global {
interface Window { appConfig: { debug: boolean } }
}
export {};
The export {} makes the file a module, which is required before declare global.
Augment versus replace
A declare module "x" block in a file with no imports replaces the module types. Put an import "x"; before the block to augment instead of replace.
Where they live
@types/* packages ship .d.ts files. Local globals go in a global.d.ts included by tsconfig. Keep declarations honest: an inaccurate .d.ts makes the checker trust a lie.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What JavaScript does the compiler emit for this file? ```ts declare function log(message: string): void; ```
Exercise 2Implement
Write a declaration for a module `tiny-math` that exports `add(a: number, b: number): number` and a default function `square(n: number): number`.
starter.typescriptdeclare module "tiny-math" { // your code }Reveal the reference answer
reference.typescriptdeclare module "tiny-math" { export function add(a: number, b: number): number; export default function square(n: number): number; }Authored expected output: Compile-time: importing from tiny-math type-checks. No runtime output.
The declaration describes the shapes the JavaScript module already provides.
Exercise 3Debug
This fails to compile: augmentations for the global scope can only be nested in external modules. Fix it. ```ts declare global { interface Window { ready: boolean } } ```
starter.typescriptdeclare global { interface Window { ready: boolean } }Reveal the reference answer
reference.typescriptexport {}; declare global { interface Window { ready: boolean } }Authored expected output: Compile error resolved: adding export {} makes the file a module and the global augmentation valid.
A file with no imports or exports is a script; declare global needs a module.
Common misconceptions
#- A .d.ts emits JavaScript.
- declare creates a runtime value.
- declare module always augments an existing module.
Follow-up practice
#- typescript-module-augmentation
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.