Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Language, Modules and Emit Options

TypeScript emitDeclarationOnly option

Reference for emitDeclarationOnly: let esbuild, SWC or Babel produce JavaScript while tsc writes only the .d.ts files, and how it combines with noEmit.

emitDeclarationOnly tells tsc to write .d.ts files (and their maps) and nothing else. No .js files are produced. It requires declaration or composite to be on.

The typical setup splits the work: a fast bundler such as esbuild, SWC or Vite turns TypeScript into JavaScript, and tsc is run once to type-check and emit the declarations that library consumers need. That gives quick builds without losing correct types.

JSON
{ "compilerOptions": { "declaration": true, "emitDeclarationOnly": true, "outDir": "dist" } }

Try it

Bundlers that strip types never check them. That is why a separate tsc run matters. This example shows code a type stripper would happily turn into JavaScript, while the type checker would object.

TypeScript Types are erased, values remain

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Declaration-only output for a class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{
  "compilerOptions": {
    "declaration": true,
    "emitDeclarationOnly": true,
    "outDir": "dist/types"
  }
}

A typical package.json script pairs the two tools:

JSON
{ "scripts": { "build": "esbuild src/index.ts --bundle --outfile=dist/index.js && tsc" } }
Name Description
emitDeclarationOnly true writes only .d.ts and .d.ts.map; false (default) also writes JavaScript.

Usage notes

  • It conflicts with noEmit: with noEmit nothing is written at all. Use noEmit for check-only runs and emitDeclarationOnly for library builds.
  • You still get full type-checking and errors; only the file output changes.
  • Combine with declarationMap so editors can jump from the .d.ts to the source.
  • With isolatedDeclarations, other tools can generate .d.ts files without the full type checker, but exported members then need explicit types.
  • Project references (composite) rely on declaration output to avoid re-checking upstream projects.