TypeScript Deep Dive
  • README
  • Getting Started
    • Why TypeScript
  • JavaScript
    • Equality
    • References
    • Null vs. Undefined
    • this
    • Closure
    • Number
    • Truthy
  • Future JavaScript Now
    • Classes
      • Classes Emit
    • Arrow Functions
    • Rest Parameters
    • let
    • const
    • Destructuring
    • Spread Operator
    • for...of
    • Iterators
    • Template Strings
    • Promise
    • Generators
    • Async Await
  • Project
    • Compilation Context
      • tsconfig.json
      • Which Files?
    • Declaration Spaces
    • Modules
      • File Module Details
      • global.d.ts
    • Namespaces
    • Dynamic Import Expressions
  • Node.js QuickStart
  • Browser QuickStart
  • Library QuickStart
  • TypeScript's Type System
    • JS Migration Guide
    • @types
    • Ambient Declarations
      • Declaration Files
      • Variables
    • Interfaces
    • Enums
    • lib.d.ts
    • Functions
    • Callable
    • Type Assertion
    • Freshness
    • Type Guard
    • Literal Types
    • Readonly
    • Generics
    • Type Inference
    • Type Compatibility
    • Never Type
    • Discriminated Unions
    • Index Signatures
    • Moving Types
    • Exception Handling
    • Mixins
  • JSX
    • React
    • Non React JSX
  • Options
    • noImplicitAny
    • strictNullChecks
  • Errors in TypeScript
    • Interpreting Errors
    • Common Errors
  • NPM
  • Testing
    • Jest
    • Cypress
  • Tools
    • Prettier
    • Husky
    • ESLint
    • Changelog
  • TIPs
    • String Based Enums
    • Nominal Typing
    • Stateful Functions
    • Currying
    • Type Instantiation
    • Lazy Object Literal Initialization
    • Classes are Useful
    • Avoid Export Default
    • Limit Property Setters
    • outFile caution
    • JQuery tips
    • static constructors
    • singleton pattern
    • Function parameters
    • Build Toggles
    • Barrel
    • Create Arrays
    • Typesafe Event Emitter
  • StyleGuide
  • TypeScript Compiler Internals
    • Program
    • AST
      • TIP: Visit Children
      • TIP: SyntaxKind enum
      • Trivia
    • Scanner
    • Parser
      • Parser Functions
    • Binder
      • Binder Functions
      • Binder Declarations
      • Binder Container
      • Binder SymbolTable
      • Binder Error Reporting
    • Checker
      • Checker Diagnostics
      • Checker Error Reporting
    • Emitter
      • Emitter Functions
      • Emitter SourceMaps
    • Contributing
Powered by GitBook
On this page
  • Syntax vs. Semantics
  • Processing Overview
  • File: Utilities
  • File: Key Data Structures
  • File: System

TypeScript Compiler Internals

PreviousStyleGuideNextProgram

Last updated 4 years ago

The TypeScript compiler source is located under the folder.

It is split into the follow key parts:

  • Scanner (scanner.ts)

  • Parser (parser.ts)

  • Binder (binder.ts)

  • Checker (checker.ts)

  • Emitter (emitter.ts)

Each of these get their own unique files in the source. These parts will be explained later on in this chapter.

Syntax vs. Semantics

Just because something is syntactically correct doesn't mean it is semantically correct. Consider the following piece of TypeScript code which although syntactically valid is semantically wrong

var foo: number = "not a number";

Semantic means "meaning" in English. This concept is useful to have in your head.

Processing Overview

The following is a quick review of how these key parts of the TypeScript compiler compose:

SourceCode ~~ scanner ~~> Token Stream
Token Stream ~~ parser ~~> AST
AST ~~ binder ~~> Symbols

Symbol is the primary building block of the TypeScript semantic system. As shown the symbols are created as a result of binding. Symbols connect declaration nodes in the AST to other declarations contributing to the same entity.

Symbols + AST are what is used by the checker to semantically validate the source code

AST + Symbols ~~ checker ~~> Type Validation

Finally When a JS output is requested:

AST + Checker ~~ emitter ~~> JS

There are a few additional files in the TypeScript compiler that provide utilities to many of these key portions which we cover next.

File: Utilities

core.ts : core utilities used by the TypeScript compiler. A few important ones:

  • let objectAllocator: ObjectAllocator : is a variable defined as a singleton global. It provides the definitions for getNodeConstructor (Nodes are covered when we look at parser / AST), getSymbolConstructor (Symbols are covered in binder), getTypeConstructor (Types are covered in checker), getSignatureConstructor (Signatures are the index, call and construct signatures).

File: Key Data Structures

types.ts contains key data structures and interfaces uses throughout the compiler. Here is a sampling of a few key ones:

  • SyntaxKind

    The AST node type is identified by the SyntaxKind enum.

  • TypeChecker

    This is the interface provided by the TypeChecker.

  • CompilerHost

    This is used by the Program to interact with the System.

  • Node

    An AST node.

File: System

system.ts. All interaction of the TypeScript compiler with the operating system goes through a System interface. Both the interface and its implementations (WScript and Node) are defined in system.ts. You can think of it as the Operating Environment (OE).

Now that you have an overview of the major files, we can look at the concept of Program

src/compiler