What is a .d.ts file and what is it used for?
Short answer
A declaration file contains only type information — interfaces, type aliases, and function/variable signatures — with no runtime implementation code.
Advertisement
AModel answer
A declaration file contains only type information — interfaces, type aliases, and function/variable signatures — with no runtime implementation code. It lets TypeScript type-check code that uses a JavaScript library, or lets consumers of your own package get type checking and autocomplete.
// mathUtils.d.ts
export function add(a: number, b: number): number;
export const PI: number;Real-world example
Providing type information for a plain JavaScript library so TypeScript users get autocomplete and type errors when using it.
Likely follow-ups
- Do .d.ts files get compiled into any JavaScript output?
Related topics