Skip to content

Declaration File

In one line

A declaration file (.d.ts) provides TypeScript type information for JavaScript code that doesn't include types, enabling type checking for the entire JS ecosystem.

In simple words

Declaration files (.d.ts) describe the types of JavaScript libraries. They contain only type information — interfaces, function signatures, type aliases — no implementation code. They are erased at compile time.

When you install @types/lodash, you are getting a .d.ts file that tells TypeScript how to use Lodash. Without it, TypeScript would treat the library as any.

You can write your own .d.ts files for internal JavaScript modules or for libraries that don't ship types. The declare keyword is used to tell TypeScript that the described entity exists at runtime, even though TypeScript can't see its implementation.

Example

typescript
// my-module.d.ts
declare module "my-module" {
  export function doSomething(x: string): number;
  export const version: string;
}

This declaration file tells TypeScript that my-module exports doSomething and version.

Related terms

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary