TExcelTAD
Home › TypeScript › TS Declaration Files

TypeScript Declaration Files

A declaration file (.d.ts) contains only types — no implementations. It tells the compiler what exists in some JavaScript code: which functions a library exports, what globals a script tag adds, which properties an object has. The .d.ts files are how TypeScript can type-check code that calls plain JavaScript.

You meet declaration files in three ways:

Source Example
Shipped with a library node_modules/zod/index.d.ts (the "types" field in package.json)
From DefinitelyTyped npm install -D @types/lodash
Written by you src/types/legacy-widget.d.ts for an untyped script

TypeScript's own knowledge of JavaScript and the browser comes from declaration files too: lib.es2023.d.ts, lib.dom.d.ts and friends.

The declare keyword

Inside a .d.ts file every statement describes something that exists elsewhere, so you write declare (or export) instead of an implementation:

ts
// globals.d.ts — describes values a <script> tag puts on the page
declare const APP_VERSION: string;
declare function trackEvent(name: string, data?: Record<string, unknown>): void;

declare namespace Analytics {
  interface Options { sampleRate: number }
  function init(opts: Options): void;
}

You can use declare in an ordinary .ts file too. It is a promise to the compiler: "this exists at runtime, trust me". The compiler then checks every use against that declaration:

ts · try it yourself
declare const BUILD_ID: string;

const id: number = BUILD_ID;
main.ts(3,7): error TS2322: Type 'string' is not assignable to type 'number'.

Be careful: declare produces no JavaScript, so if the value doesn't really exist you get a runtime ReferenceError. Here we create it on globalThis first, so the declared name really is there when the code runs:

ts · try it yourself
(globalThis as any).BUILD_ID = "2026.10-rc1";

declare const BUILD_ID: string;
console.log(`Build ${BUILD_ID.toUpperCase()}`);
Build 2026.10-RC1

Typing a JavaScript module

Suppose your project still has an old slugify.js file:

js
// slugify.js
export function slugify(text, sep = "-") {
  return text.toLowerCase().trim().replace(/\s+/g, sep);
}
export const VERSION = "1.2.0";

Put a slugify.d.ts next to it with the same base name. TypeScript picks it up automatically whenever something imports ./slugify.js:

ts
// slugify.d.ts
export declare function slugify(text: string, sep?: string): string;
export declare const VERSION: string;

Now slugify(42) in a .ts file is a compile error, and editors show the parameter names and types on hover.

Declaring a whole package: declare module

For an npm package without types (and no @types package), write a module declaration in any .d.ts file included by your tsconfig:

ts
// types/tiny-colors.d.ts
declare module "tiny-colors" {
  export function hex(r: number, g: number, b: number): string;
  export default function parse(color: string): [number, number, number];
}

// a quick escape hatch: everything from this package is `any`
declare module "untyped-lib";

Wildcards describe non-code imports that bundlers handle, such as images or CSS modules:

ts
// assets.d.ts
declare module "*.svg" {
  const url: string;
  export default url;
}
declare module "*.module.css" {
  const classes: Record<string, string>;
  export default classes;
}

Augmenting existing types

Interfaces with the same name merge. That lets a declaration file add members to types defined elsewhere — even global ones. Inside a module, wrap global additions in declare global:

ts · try it yourself
export {};

declare global {
  interface Array<T> {
    last(): T | undefined;
  }
}

Array.prototype.last = function () {
  return this[this.length - 1];
};

console.log([3, 1, 4].last());
console.log([].last());
4
undefined

Tip: Patching built-in prototypes is fine for learning how augmentation works, but in real projects prefer a helper function such as last(arr) — prototype changes affect every library on the page.

The same merging works for library types, which is how plugins extend a library's interfaces:

ts
// express-augment.d.ts
import "express";
declare module "express" {
  interface Request {
    userId?: string;   // added by our auth middleware
  }
}

Generating declaration files

When you publish your own TypeScript library, let the compiler write the .d.ts files for you:

json
{
  "compilerOptions": {
    "declaration": true,
    "declarationMap": true,
    "outDir": "dist"
  }
}
bash · try it yourself
npx tsc && ls dist
index.d.ts
index.d.ts.map
index.js

Set "types": "./dist/index.d.ts" in package.json so consumers get the types automatically.

Test yourself

What does a .d.ts file contain?

  1. Compiled JavaScript
  2. Only type declarations, no implementations
  3. Unit tests
  4. Minified TypeScript

What happens at runtime if you declare const X: string but nothing defines X?

  1. X is undefined
  2. TypeScript creates it
  3. A ReferenceError when X is used
  4. A compile error

How do you add a method to the global Array<T> interface from inside a module?

  1. declare global { interface Array<T> { … } }
  2. extend Array { … }
  3. export interface Array<T> { … }
  4. It is impossible