← Back to TypeScript Course | Chapter 11: Type Declarations and DefinitelyTyped | Lesson 1 of 5

.d.ts Declaration Files

A .d.ts file contains TypeScript declarations that describe existing JavaScript code without providing its runtime implementation. Declaration files help TypeScript understand libraries, APIs, and external code.
Syntax
typescript
declare function name(param: type): returnType;
declare const value: type;
export interface Name {}

What Is a Declaration File

A .d.ts declaration file describes the shape of a library's types — its functions, classes, and variables — without containing any actual runtime implementation, letting TypeScript type-check code that calls into plain JavaScript.

Example: What Is a Declaration File

typescript
// math-lib.d.ts (no runtime code, only shapes)
declare function add(a: number, b: number): number;
// The real implementation lives in math-lib.js
console.log("Declaration file describes add() without implementing it");

Ambient Declarations

Ambient declarations (using the declare keyword) tell the compiler that something exists at runtime — like a global variable injected by a script tag — even though TypeScript can't see where it's actually defined.

Example: Ambient Declarations

typescript
declare const APP_VERSION: string;
// Assume a <script> tag defines APP_VERSION globally at runtime.
console.log("Ambient declaration for a global injected elsewhere");

Declaring Classes

Declaring a class in a .d.ts file lists its constructor signature, public members, and their types so consumers get autocomplete and type-checking against a class whose real implementation lives in a separate .js file.

Example: Declaring Classes

typescript
declare class EventEmitter {
  on(event: string, handler: Function): void;
  emit(event: string): void;
}
// Real implementation lives in a separate .js file.
console.log("Class shape declared for a JS implementation");

Declaring Functions

Declaring a function's signature in a .d.ts file — its parameter types and return type — is enough for the compiler to catch misuse at call sites, even without seeing a single line of that function's actual logic.

Example: Declaring Functions

typescript
declare function parseCsv(input: string): string[][];
// Only the signature is known here, not the logic.
console.log("Function signature declared, catches misuse at call sites");

When Declaration Files Are Used

Declaration files are typically used when shipping a JavaScript library to TypeScript consumers, when writing types for a global script that isn't a module, or when the DefinitelyTyped project hasn't yet published types for a package.

Example: When Declaration Files Are Used

typescript
// Used when: shipping a JS library, typing a global script,
// or DefinitelyTyped hasn't published types yet.
declare function legacyWidget(id: string): void;
console.log("Declaration files bridge JS libraries into TypeScript");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Putting real code in a .d.ts file, like function add(a, b) { return a + b; }, when it can only contain declarations.
  2. Expecting the .d.ts file to create the runtime function, so calling it fails with ReferenceError.
  3. Forgetting to include the .d.ts file in tsconfig.json (include / typeRoots), so the compiler cannot find it.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.