The type checker is the semantic analysis core of the TypeScript compiler. It consumes the AST produced by the parser and the symbol table built by the binder, then performs symbol resolution, type inference, type compatibility checking, and generates all semantic diagnostics. The type checker is implemented in src/compiler/checker.ts, which is by far the largest file in the repository (~70,000 lines).
For parser and AST details, see 2.2 For binder and symbol table construction, see 2.3 For how Program manages the checker lifecycle, see 2.6 For the public-facing TypeScript compiler API, see 7
The checker is created lazily by Program.getTypeChecker() via createTypeChecker in src/compiler/program.ts src/compiler/program.ts50 It wraps all bound source files from the program and performs semantic analysis on demand.
Compilation Pipeline — Code Entity Mapping
Sources: src/compiler/program.ts50 src/compiler/checker.ts1-1151 src/compiler/emitter.ts1-426
createTypeCheckercreateTypeChecker(host: TypeCheckerHost): TypeChecker is the sole export from src/compiler/checker.ts that external consumers use src/compiler/checker.ts1157 It returns an object implementing:
TypeChecker — the public interface defined in src/compiler/types.ts src/compiler/types.ts4012-4144 consumed by the language service and public API.EmitResolver — an internal interface consumed by the emitter and declaration emitter to answer questions about symbol visibility, constant values, and emit-relevant type information src/compiler/types.ts4152-4214The factory closes over all internal state: relation maps, type and symbol caches, diagnostic arrays, and configuration derived from CompilerOptions.
Sources: src/compiler/checker.ts1-1151 src/compiler/program.ts50 src/compiler/types.ts4012-4214
Type Interface and TypeFlagsEvery type in the TypeScript type system is represented by the Type interface in src/compiler/types.ts src/compiler/types.ts3661-3666 The flags: TypeFlags bitmask field identifies the exact variety of each type instance src/compiler/types.ts3581-3659
Type Hierarchy via TypeFlags
Key TypeFlags groupings in src/compiler/types.ts:
| Group | Members |
|---|---|
Nullable | `Undefined |
StringLike | `String |
NumberLike | `Number |
StructuredType | `Object |
Instantiable | `TypeParameter |
Narrowable | `Any |
Sources: src/compiler/types.ts3581-3666 src/compiler/checker.ts459-1150
ObjectFlagsObject types carry a second bitmask, objectFlags: ObjectFlags, that further classifies them src/compiler/types.ts3692-3715 Key values:
| Flag | Meaning |
|---|---|
Reference | Instantiation of a generic type (e.g. Array<string>) src/compiler/types.ts3695 |
Tuple | Tuple type src/compiler/types.ts3696 |
Anonymous | Object literal type or function type src/compiler/types.ts3697 |
Mapped | Mapped type src/compiler/types.ts3698 |
ObjectLiteral | Fresh object literal type src/compiler/types.ts3701 |
EvolvingArray | Narrowing-evolved array type src/compiler/types.ts3702 |
Sources: src/compiler/types.ts3692-3715 src/compiler/checker.ts930-940
Symbol and SymbolFlagsSymbol objects are created by the binder and decorated by the checker src/compiler/types.ts3522-3532 The flags: SymbolFlags bitmask identifies what kind of entity a symbol represents src/compiler/types.ts3464-3518
| Flag Category | Members |
|---|---|
| Value declarations | FunctionScopedVariable, BlockScopedVariable, Property, EnumMember, Function, Class, Enum, ValueModule, GetAccessor, SetAccessor, Method, Constructor src/compiler/types.ts3510 |
| Type declarations | Interface, TypeAlias, TypeParameter, TypeLiteral src/compiler/types.ts3511 |
| Alias | Alias (imports, re-exports) src/compiler/types.ts3489 |
| Transient | Transient (checker-synthesized, e.g. mapped type members) src/compiler/types.ts3503 |
Sources: src/compiler/types.ts3464-3532 src/compiler/checker.ts256-262
The checker avoids redundant computation by associating cached data with nodes and symbols:
NodeLinks — attached to AST nodes via a node id. Stores: resolved type (resolvedType), resolved signature, and flags like NodeCheckFlags src/compiler/types.ts3944-3984SymbolLinks — attached to Symbol objects. Stores: declared type, resolved type, and more src/compiler/types.ts3914-3942NodeCheckFlags encodes semantic properties discovered during checking, such as whether a super call is needed or whether a binding is captured across a closure boundary src/compiler/types.ts3986-4010
Sources: src/compiler/types.ts3914-4010 src/compiler/checker.ts916-920
getTypeOfExpression and CheckModeThe central internal function for deriving a type from an expression node is getTypeOfExpression. It delegates to checkExpression, which dispatches based on the expression's SyntaxKind. The CheckMode const enum controls how the check proceeds src/compiler/checker.ts1157-1259:
CheckMode | Description |
|---|---|
Normal | Standard type checking src/compiler/checker.ts1158 |
Contextual | Deriving a contextual type from a parent expression src/compiler/checker.ts1160 |
Inferential | Type inference from an argument against a parameter type src/compiler/checker.ts1161 |
Expression Type Resolution Flow
Sources: src/compiler/checker.ts1157-1259 src/compiler/types.ts453-456
InferenceContext and InferencePriorityWhen the checker resolves a generic call expression, it creates an InferenceContext src/compiler/types.ts3849-3860 InferencePriority ranks candidates src/compiler/types.ts3827-3847:
| Priority | Meaning |
|---|---|
NakedTypeVariable | Direct inference against a type parameter src/compiler/types.ts3828 |
ReturnType | From return type src/compiler/types.ts3838 |
AlwaysStrict | Strict inference mode src/compiler/types.ts3845 |
Sources: src/compiler/types.ts3827-3860 src/compiler/checker.ts453-456
The checker maintains several objects that cache the result of comparing two types. These are the key relations:
| Relation | Use |
|---|---|
assignableRelation | Is type S assignable to type T? src/compiler/checker.ts467 |
strictSubtypeRelation | Is S a strict subtype of T? src/compiler/checker.ts469 |
comparableRelation | Is S comparable to T? src/compiler/checker.ts468 |
identityRelation | Are S and T identical? src/compiler/checker.ts466 |
Assignability Checking Flow
Sources: src/compiler/checker.ts466-469 src/compiler/types.ts1-20
The checker uses the flow graph built by the binder to compute narrowed types for variables at specific AST locations.
TypeFactsThe TypeFacts bitmask describes which type guards can narrow a given type src/compiler/checker.ts1229-1259
| Group | Meaning |
|---|---|
Truthy, Falsy | Truthiness guards src/compiler/checker.ts1255-1256 |
IsUndefined, IsNull | Type contains undefined/null src/compiler/checker.ts1258-1259 |
Sources: src/compiler/checker.ts1229-1259
TypeChecker InterfaceThe TypeChecker interface in src/compiler/types.ts is what the language service and external consumers use src/compiler/types.ts4012-4144
Type Query Methods
| Method | Description |
|---|---|
getTypeAtLocation(node) | Type of an expression or declaration node src/compiler/types.ts4013 |
getSymbolAtLocation(node) | Symbol referenced by a node src/compiler/types.ts4017 |
getResolvedSignature(call) | Resolved Signature for a call expression src/compiler/types.ts4043 |
getSemanticDiagnostics(sourceFile?) | All semantic diagnostics src/compiler/types.ts4139 |
Sources: src/compiler/types.ts4012-4144 tests/baselines/reference/api/typescript.d.ts1-200
Refresh this wiki