TypeScript Reference ยท Keywords and Declarations
The declare keyword
Reference for TypeScript declare: describe values, functions, classes, modules and globals that exist at runtime without emitting any JavaScript code.
declare tells the compiler that something exists at runtime but is defined elsewhere. The declaration is ambient: it gives a name and a type, produces no JavaScript, and cannot contain an implementation. You use it for globals injected by a script or the host environment, for libraries that ship without types, and inside .d.ts files.
Because the compiler trusts an ambient declaration completely, a wrong one is a lie that no check will catch. Keep declarations narrow and put them in one place.
declare const BUILD_ID: string; declare function legacyAlert(message: string): void;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
declare var name: Type; declare let name: Type; declare const name: Type; declare function f(a: A): R; declare class C { method(): void; } declare namespace N { const x: number; } declare enum E { A } declare module "pkg" { export function f(): void; } declare module "*.svg" { const url: string; export default url; } declare global { interface Window { app: string } } class K { declare field: number; }
| Form | Description |
|---|---|
declare const/let/var |
A global variable. No initializer is allowed, except a literal for const. |
declare function |
A function signature with no body. Overloads are written as several declarations. |
declare class |
Members have types but no bodies or initializers. |
declare module "name" |
Describes or augments an external module. A pattern with * matches many specifiers. |
declare global |
Adds to the global scope from inside a module file. The file must have a top-level import or export. |
declare field |
In a normal class, states that a field exists without emitting its initialization. |
Edge cases and errors:
- Inside a
.d.tsfile, top-level declarations are implicitly ambient, sodeclareis required except forinterfaceandtype. - An ambient declaration cannot have an implementation: a function body there is an error.
- A
declareclass field does not get initialized byuseDefineForClassFields, which is its main purpose: it avoids overwriting a value that a base class or decorator already set.
Usage notes
declarenever changes runtime behavior. If the value is not actually there, you get aReferenceErrorat run time, not a compile error.- Prefer a typed import from a real package or
@types/*over hand-written declarations when one exists. declare module "*.css"style wildcards type asset imports handled by a bundler.- Module augmentation (
declare module "express" { ... }) merges new members into an existing module's types. The file must itself be a module. - Use
declare globalinstead of editing built-in lib files. - A
declare namespacewith only types is the usual way to describe a UMD global.