Explore how the TypeScript compiler works internally — from source code to JavaScript — by understanding Lexer, Parser, Binder, Checker, and Emitter
🚀 Introduction
TypeScript is widely used for building scalable and maintainable JavaScript applications.
But have you ever wondered what actually happens behind the scenes when TypeScript code is compiled?
The TypeScript compiler follows a well-defined pipeline that converts .ts files into plain JavaScript.
Understanding this internal workflow helps developers:
- Debug type-related issues more effectively
- Write better, predictable TypeScript code
- Understand compiler errors deeply instead of guessing
In this article, we’ll break down the TypeScript compilation process step by step.
🧠 TypeScript Compilation Overview
When you run the TypeScript compiler (tsc), it does much more than just removing types.
The process is divided into these main phases:
- Lexer (Tokenizer)
- Parser
- Binder
- Type Checker
- Transformer
- Emitter
Each phase has a clear responsibility, and together they convert TypeScript into JavaScript.
1. Lexer (Tokenizer)
The Lexer is the first stage of the compiler.
What does it do?
- Reads raw TypeScript source code
- Breaks it into small units called tokens
- Ignores whitespace and comments
Example
let count: number = 10;
Lexer converts to tokens:
["let", "x", ":", "number", "=", "10", ";"]
Why important: It simplifies code for the parser.
2. Parser (AST Builder)
Parser converts tokens into an AST (Abstract Syntax Tree).
Example AST structure:
VariableStatement
└─ VariableDeclaration
├─ name: x
├─ type: number
└─ initializer: 10
Why important: All further operations (type checking, transformation) run on this AST.
3. Binder (Scope & Symbol Table Maker)
The binder walks the AST and creates:
✔ Scopes
✔ Symbol tables
✔ Variable/function references
Scope 1 (global)
└─ symbol: x → type:number
Why important: The type checker needs this to resolve identifiers.
4. Type Checker
This is the heart of TypeScript.
✔ Type inference
✔ Type compatibility checks
✔ Error reporting
✔ Resolve generics
✔ Check function signatures
✔ Check return types
✔ Narrowing, control-flow analysis
Example error:
let x: number = "hello";
Type checker throws:
Type '"hello"' is not assignable to type 'number'.
5. Transformer (TS → JS Conversion)
Transforms TypeScript AST to JavaScript AST.
Example input:
class User {
constructor(public name: string) {}
}
Transform output:
"use strict";
class User {
constructor(name) {}
}
6. Emitter (Generate Final JavaScript + Source Maps)
Finally, JavaScript code is printed.
Outputs:
.js
.js.map
.d.ts (if enabled)

Top comments (0)