Careeroza
What are d.ts Files
Types Without Code
A declaration file (.d.ts) contains only types: no implementation. It describes JavaScript code that lives somewhere else. When you publish a library, tsc --declaration generates .d.ts files next to the .js files so TypeScript users get types.
src/price.ts
export function withGst(amount: number, rate = 0.18): number {
return Math.round(amount * (1 + rate));
}Generated dist/price.js and dist/price.d.ts:
function withGst(amount, rate = 0.18) {
return Math.round(amount * (1 + rate));
}
exports.withGst = withGst;export declare function withGst(amount: number, rate?: number): number;Where Types Come From
| Source | Example |
|---|---|
Built into TypeScript (lib) | lib.es2023.d.ts, lib.dom.d.ts: Array, Promise, document |
| Shipped with the package | axios, zod, next (look for "types" in package.json) |
DefinitelyTyped @types/* | @types/node, @types/express, @types/lodash |
Your own .d.ts | untyped internal JS, global variables, asset imports |