The Emitter
The emitter is the final stage of compilation, responsible for generating output files. Located insrc/compiler/emitter.ts (273KB), it transforms the type-checked AST into JavaScript and declaration files.
Emit Entry Point
The emitter uses the resolver to query type information and applies transformers to modify the output.
Emit Process
Output Formats
TypeScript can emit different JavaScript module formats:Module Formats
- CommonJS
- ES Modules
- UMD
- System
Node.js style modules:Config:
Emit Targets
Thetarget option controls which JavaScript version is emitted:
ES3
Ancient JavaScript (IE8 and below)
ES5
Modern legacy (IE9-11)
ES2015+
Modern JavaScript
ESNext
Latest features
Target Impact on Output
- Classes
- Arrow Functions
- Async/Await
- Destructuring
ES2015+:ES5:
Declaration Files
Declaration files (.d.ts) contain type information without implementation:
Generating Declarations
declaration
Generate
.d.ts files alongside JavaScriptdeclarationMap
Generate source maps for declaration files
emitDeclarationOnly
Emit only
.d.ts files (skip JavaScript)Declaration File Example
Source Maps
Source maps enable debugging TypeScript code in browsers:Source Map Configuration
sourceMap
sourceMap
Generate separate
.js.map files:inlineSourceMap
inlineSourceMap
Embed source map in JavaScript file:
inlineSources
inlineSources
Include original TypeScript source in source map:
Transformers
Transformers modify the AST before emission:Built-in Transformers
- Type Erasure
- Enum Transformation
- Namespace Transformation
- Decorator Transformation
Removes TypeScript-specific syntax:
Custom Transformers
Emit Options
Output Control
outDir
Directory for output files
outFile
Concatenate output into single file
removeComments
Strip comments from output
noEmit
Skip emit entirely (type checking only)
noEmitOnError
Don’t emit if there are errors
JavaScript Emit Options
importHelpers
importHelpers
Import helper functions from
tslib instead of inlining:downlevelIteration
downlevelIteration
Emit more accurate iteration for ES5/ES3:
preserveConstEnums
preserveConstEnums
Keep
const enum declarations in output:Emit Helpers
TypeScript uses helper functions for downleveling:Incremental Emit
TypeScript can store compilation state for faster rebuilds:.tsbuildinfo file contains:
- File hashes
- Dependency graph
- Emit signatures
- Diagnostic information
Incremental compilation can dramatically speed up rebuilds by only recompiling changed files and their dependents.
Best Practices
Match Target to Environment
Set
target based on your runtime:Enable Source Maps
Always generate source maps for debugging:
Use Declaration Maps
For libraries, enable declaration maps:
Optimize Bundle Size
Use
importHelpers to reduce duplication:Related Topics
Compiler Overview
Learn about the full compilation pipeline
Type Checking
Understand type validation