Introduction
The TypeScript codebase is organized into a modular architecture designed to support both command-line compilation and rich IDE integration. This document provides an overview of the major components and their relationships.Directory Structure
The TypeScript source code is organized undersrc/ with the following key directories:
Core Compiler (src/compiler/)
Scanner & Parser
Lexical analysis and syntax tree construction
Binder
Symbol creation and scope resolution
Checker
Type checking and semantic analysis
Emitter
JavaScript code generation
scanner.ts(4,101 lines) - Tokenizes source codeparser.ts(10,823 lines) - Builds Abstract Syntax Treesbinder.ts(3,913 lines) - Creates symbols and control flowchecker.ts(54,434 lines) - The type checker (largest file)emitter.ts(6,378 lines) - Generates JavaScript outputtypes.ts- Core type definitions and SyntaxKind enumutilities.ts- Shared utility functions
Language Services (src/services/)
The services layer provides IDE features like autocompletion, navigation, and refactoring.
completions.ts- Autocompletion enginegoToDefinition.ts- Symbol navigationfindAllReferences.ts- Symbol usage trackingrename.ts- Symbol renamingdocumentHighlights.ts- Occurrence highlighting
formatting/- Code formatting rulescodefixes/- Quick fixes for diagnosticsrefactors/- Refactoring operationsorganizeImports.ts- Import statement organization
callHierarchy.ts- Call graph navigationinlayHints.ts- Inline type hintsnavigationBar.ts- File outline view
Language Server (src/server/ & src/tsserver/)
TSServer Protocol
The language server implements the editor communication protocol defined in
protocol.ts- Request/response handling
- Session management
- Project coordination
Supporting Modules
| Directory | Purpose |
|---|---|
src/typescript/ | Public API exports |
src/tsc/ | Command-line compiler |
src/typingsInstaller/ | Automatic type acquisition |
src/harness/ | Test infrastructure |
src/lib/ | Built-in type definitions |
Component Relationships
Compilation Pipeline
The compilation process follows this flow:Language Service Architecture
The language service layer sits on top of the compiler and provides incremental, interactive features:Key Design Principles
Immutable Data Structures
Immutable Data Structures
AST nodes are immutable. Transformations create new nodes rather than modifying existing ones.
Lazy Evaluation
Lazy Evaluation
Type checking and symbol resolution happen on-demand to support incremental compilation.
Position-Based APIs
Position-Based APIs
Language service features work with text positions rather than requiring full AST traversal.
Visitor Pattern
Visitor Pattern
The
forEachChild function enables efficient AST traversal and transformation.Program and Type Checker
TheProgram interface (src/compiler/program.ts) is the central coordinator:
Program
- Manages source files
- Coordinates module resolution
- Provides access to TypeChecker
- Handles diagnostics collection
TypeChecker
- Performs type inference
- Resolves symbols
- Checks type compatibility
- Reports semantic errors
Transformers
Thesrc/compiler/transformers/ directory contains transformation pipelines:
- ES downleveling:
es2015.ts,es2016.ts,es2017.ts, etc. - Feature transforms:
jsx.ts,generators.ts,decorators.ts - Module transforms:
module/directory for various module formats
Transformers convert modern TypeScript/JavaScript syntax into code compatible with older runtimes.
Public API Surface
Thesrc/typescript/ directory defines the public API exported to consumers:
Development Entry Points
Next Steps
Compiler Internals
Deep dive into scanner, parser, binder, checker, and emitter
Language Service
How IDE features like completions and navigation work