Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
declare const BUILD_ID: string;
declare function legacyAlert(message: string): void;

Try it

TypeScript Declaring a global that the host provides

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.

TypeScript Extending global and class types

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

TypeScript
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.ts file, top-level declarations are implicitly ambient, so declare is required except for interface and type.
  • An ambient declaration cannot have an implementation: a function body there is an error.
  • A declare class field does not get initialized by useDefineForClassFields, which is its main purpose: it avoids overwriting a value that a base class or decorator already set.

Usage notes

  • declare never changes runtime behavior. If the value is not actually there, you get a ReferenceError at 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 global instead of editing built-in lib files.
  • A declare namespace with only types is the usual way to describe a UMD global.