Skip to content

TypeScript Declaration Files (.d.ts)

Declaration files (.d.ts) describe the shape of existing JavaScript code to TypeScript. They contain only type information — no implementation.

globals.d.ts
declare const API_VERSION: string;
declare function fetchData(url: string): Promise<unknown>;
interface Window {
__APP_CONFIG__: { apiUrl: string; debug: boolean };
}
my-library.d.ts
declare module "my-library" {
export function doSomething(input: string): number;
export interface Options {
timeout: number;
retries: number;
}
}
// Add types to existing modules
import "express";
declare module "express" {
interface Request {
user?: { id: string; role: string };
}
}
tsconfig.json
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./types",
"emitDeclarationOnly": true
}
}

Easy: What is a .d.ts file?

Medium: How do you create module declarations for a JavaScript library?

Hard: How do you augment an existing module’s types?