Ship public type contracts alongside compiled JavaScript.
Library publishing
TypeScript · Const
The declaration compiler option generates .d.ts files that describe a project’s external API. Consumers and editor tools can use them for type checking and IntelliSense without inspecting implementation code.
The declaration compiler option asks TypeScript to generate a .d.ts file for each project source file. These files describe the module’s externally visible types, support editor tooling and checking by consumers, and contain no executable implementation.
{"compilerOptions":{"declaration":true}}| Name | Type | Description |
|---|---|---|
declaration | boolean | Boolean compiler option. When true, generates .d.ts files for TypeScript or JavaScript project inputs. |
Generates .d.ts declaration files describing each input module’s external API; it does not return a runtime value.
declaration: true in compiler options; the command-line equivalent is --declaration.emitDeclarationOnly or outDir so declaration generation does not overwrite JavaScript output.export const greet = (name: string): string => `Hello, ${name}`;
Compile with tsc --declaration to emit a JavaScript file and a corresponding declaration file describing the exported API.
Library publishing
Large monorepos
JavaScript migration
declaration compiler option do?Set declaration to true in the compiler options, then TypeScript emits a .d.ts file for each TypeScript or JavaScript input file. The declarations describe the module’s external API and enable accurate checking and editor support for consumers.
Follow-up: How can a JavaScript project avoid replacing its JavaScript output? Use emitDeclarationOnly or configure outDir when generating declarations.
A .d.ts file contains type information rather than executable implementation. TypeScript and editor tools use it to understand a library’s exported API without analyzing the library’s source implementation.
Follow-up: What should a declaration contain for a module? The types of the things it exports—the public API—rather than private implementation details.
Run the compiler with --declaration, or set declaration: true in a project configuration. For each source file, TypeScript can then emit a matching declaration file alongside the JavaScript output.
Follow-up: What does emitDeclarationOnly change? It emits declaration files without emitting JavaScript, which is useful when JavaScript is produced by another tool.
emitDeclarationOnlyoutDir.d.ts filesTypeScript compilerexternal API