Skip to main content

The Emitter

The emitter is the final stage of compilation, responsible for generating output files. Located in src/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

1

Transform AST

Apply transformers to modify the syntax tree:
2

Generate Code

Convert AST nodes to JavaScript text:
3

Create Declaration Files

Generate .d.ts files for type information:
4

Generate Source Maps

Create mappings between output and source:

Output Formats

TypeScript can emit different JavaScript module formats:

Module Formats

Node.js style modules:
Config:

Emit Targets

The target 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

ES2015+:
ES5:

Declaration Files

Declaration files (.d.ts) contain type information without implementation:

Generating Declarations

declaration

Generate .d.ts files alongside JavaScript

declarationMap

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

Generate separate .js.map files:
Embed source map in JavaScript file:
Include original TypeScript source in source map:

Transformers

Transformers modify the AST before emission:

Built-in Transformers

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

Import helper functions from tslib instead of inlining:
Emit more accurate iteration for ES5/ES3:
Keep const enum declarations in output:

Emit Helpers

TypeScript uses helper functions for downleveling:
Use importHelpers: true to import from tslib instead of duplicating helpers:

Incremental Emit

TypeScript can store compilation state for faster rebuilds:
The .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:

Compiler Overview

Learn about the full compilation pipeline

Type Checking

Understand type validation