How to generate declaration files

· Category: TypeScript

Short answer

Set declaration: true in tsconfig.json to emit .d.ts files alongside compiled JavaScript, exposing type information to consumers.

Steps

  1. Enable declaration in tsconfig.json.
  2. Run tsc to compile your project.
  3. TypeScript generates .d.ts files in the outDir next to the .js files.
  4. For JavaScript libraries, write .d.ts files manually or use tsc --allowJs --declaration --emitDeclarationOnly.
  5. Reference the declarations in package.json via types or typings.

Tips

  • Use declarationMap: true to generate .d.ts.map files for go-to-definition across packages.
  • Keep public APIs clean to produce readable and useful declaration files.

Common issues

  • Private types leaked in public APIs cause declaration emit errors.
  • Complex inferred types may generate verbose or overly generic declarations.