0
# Type Checker
1
2
Comprehensive type analysis system that provides semantic information about TypeScript code. Powers features like type inference, error detection, and symbol resolution.
3
4
## Capabilities
5
6
### Type Checker Creation
7
8
Create a type checker instance for semantic analysis.
9
10
```typescript { .api }
11
/**
12
* Create a TypeScript type checker
13
* @param host - Type checker host providing program information
14
* @returns Type checker instance
15
*/
16
function createTypeChecker(host: TypeCheckerHost): TypeChecker;
17
18
interface TypeCheckerHost {
19
getCompilerOptions(): CompilerOptions;
20
getSourceFiles(): readonly SourceFile[];
21
getSourceFile(fileName: string): SourceFile | undefined;
22
getResolvedTypeReferenceDirectives(): ReadonlyMap<ResolvedTypeReferenceDirective | undefined>;
23
getCurrentDirectory(): string;
24
}
25
```
26
27
### Type Analysis
28
29
Get type information for AST nodes and symbols.
30
31
```typescript { .api }
32
interface TypeChecker {
33
/**
34
* Get the type of an AST node at a specific location
35
* @param node - AST node to analyze
36
* @returns Type information for the node
37
*/
38
getTypeAtLocation(node: Node): Type;
39
40
/**
41
* Get the contextual type for a node (e.g., from assignment target)
42
* @param node - AST node to analyze
43
* @returns Contextual type if available
44
*/
45
getContextualType(node: Expression): Type | undefined;
46
47
/**
48
* Get the type of a symbol at a specific location
49
* @param symbol - Symbol to analyze
50
* @param location - AST node location
51
* @returns Type of the symbol
52
*/
53
getTypeOfSymbolAtLocation(symbol: Symbol, location: Node): Type;
54
55
/**
56
* Get the base type of a type (e.g., Array<string> -> Array<T>)
57
* @param type - Type to analyze
58
* @returns Base type
59
*/
60
getBaseTypeOfLiteralType(type: Type): Type;
61
62
/**
63
* Get the apparent type (after applying index signatures, etc.)
64
* @param type - Type to analyze
65
* @returns Apparent type
66
*/
67
getApparentType(type: Type): Type;
68
}
69
```
70
71
**Usage Examples:**
72
73
```typescript
74
import * as ts from "typescript";
75
76
// Create program and type checker
77
const program = ts.createProgram(["example.ts"], {
78
target: ts.ScriptTarget.ES2020,
79
module: ts.ModuleKind.CommonJS
80
}, compilerHost);
81
82
const typeChecker = program.getTypeChecker();
83
const sourceFile = program.getSourceFile("example.ts")!;
84
85
// Get type at specific node
86
function visitNode(node: ts.Node) {
87
if (ts.isVariableDeclaration(node) && node.name) {
88
const type = typeChecker.getTypeAtLocation(node.name);
89
const typeString = typeChecker.typeToString(type);
90
console.log(`Variable ${node.name.text} has type: ${typeString}`);
91
}
92
ts.forEachChild(node, visitNode);
93
}
94
visitNode(sourceFile);
95
```
96
97
### Symbol Analysis
98
99
Work with symbols representing declarations and their metadata.
100
101
```typescript { .api }
102
interface TypeChecker {
103
/**
104
* Get the symbol at a specific AST location
105
* @param node - AST node to analyze
106
* @returns Symbol if found
107
*/
108
getSymbolAtLocation(node: Node): Symbol | undefined;
109
110
/**
111
* Get all symbols accessible in scope at a location
112
* @param location - AST node location
113
* @param meaning - Type of symbols to find (value, type, namespace)
114
* @returns Array of accessible symbols
115
*/
116
getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[];
117
118
/**
119
* Get exported symbols from a module
120
* @param module - Module symbol to analyze
121
* @returns Exported symbols
122
*/
123
getExportsOfModule(module: Symbol): Symbol[];
124
125
/**
126
* Get local symbols from a symbol (for namespaces, classes, etc.)
127
* @param symbol - Symbol to analyze
128
* @returns Local symbols
129
*/
130
getLocalSymbolsOfContainer(symbol: Symbol): Symbol[];
131
132
/**
133
* Get the fully qualified name of a symbol
134
* @param symbol - Symbol to analyze
135
* @returns Fully qualified name
136
*/
137
getFullyQualifiedName(symbol: Symbol): string;
138
}
139
```
140
141
### Signature Analysis
142
143
Analyze function and method signatures.
144
145
```typescript { .api }
146
interface TypeChecker {
147
/**
148
* Get signature from a declaration
149
* @param declaration - Function/method declaration
150
* @returns Signature information
151
*/
152
getSignatureFromDeclaration(declaration: SignatureDeclaration): Signature | undefined;
153
154
/**
155
* Get resolved signature from a call expression
156
* @param node - Call expression node
157
* @returns Resolved signature
158
*/
159
getResolvedSignature(node: CallLikeExpression): Signature | undefined;
160
161
/**
162
* Get all signatures for a type (overloads)
163
* @param type - Type to analyze
164
* @param kind - Signature kind (call, construct, index)
165
* @returns Array of signatures
166
*/
167
getSignaturesOfType(type: Type, kind: SignatureKind): readonly Signature[];
168
169
/**
170
* Get return type of a signature
171
* @param signature - Signature to analyze
172
* @returns Return type
173
*/
174
getReturnTypeOfSignature(signature: Signature): Type;
175
176
/**
177
* Check if signature has a rest parameter
178
* @param signature - Signature to check
179
* @returns True if has rest parameter
180
*/
181
hasRestParameter(signature: Signature): boolean;
182
}
183
```
184
185
### Type Utilities
186
187
Essential type analysis methods available on the TypeChecker interface.
188
189
```typescript { .api }
190
interface TypeChecker {
191
/**
192
* Get the type of a symbol
193
* @param symbol - Symbol to analyze
194
* @returns Type of the symbol
195
*/
196
getTypeOfSymbol(symbol: Symbol): Type;
197
198
/**
199
* Get the declared type of a symbol (before any type resolution)
200
* @param symbol - Symbol to analyze
201
* @returns Declared type of the symbol
202
*/
203
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
204
205
/**
206
* Get all properties of a type
207
* @param type - Type to analyze
208
* @returns Array of property symbols
209
*/
210
getPropertiesOfType(type: Type): Symbol[];
211
212
/**
213
* Get a specific property of a type by name
214
* @param type - Type to analyze
215
* @param propertyName - Name of the property
216
* @returns Property symbol if found
217
*/
218
getPropertyOfType(type: Type, propertyName: string): Symbol | undefined;
219
220
/**
221
* Get index info for a type (for string or number indexing)
222
* @param type - Type to analyze
223
* @param kind - Index kind (string or number)
224
* @returns Index info if available
225
*/
226
getIndexInfoOfType(type: Type, kind: IndexKind): IndexInfo | undefined;
227
228
/**
229
* Get all index infos for a type
230
* @param type - Type to analyze
231
* @returns Array of index infos
232
*/
233
getIndexInfosOfType(type: Type): readonly IndexInfo[];
234
235
/**
236
* Get the awaited type for async/await scenarios
237
* @param type - Type to analyze (usually a Promise type)
238
* @returns Awaited type or undefined
239
*/
240
getAwaitedType(type: Type): Type | undefined;
241
242
/**
243
* Get nullable version of a type
244
* @param type - Base type
245
* @param flags - Type flags for nullability
246
* @returns Nullable type
247
*/
248
getNullableType(type: Type, flags: TypeFlags): Type;
249
250
/**
251
* Get non-nullable version of a type
252
* @param type - Potentially nullable type
253
* @returns Non-nullable type
254
*/
255
getNonNullableType(type: Type): Type;
256
257
/**
258
* Get augmented properties of a type (including inherited and mixed-in)
259
* @param type - Type to analyze
260
* @returns Array of all property symbols
261
*/
262
getAugmentedPropertiesOfType(type: Type): Symbol[];
263
}
264
```
265
266
### Type Conversions
267
268
Convert between types and AST representations.
269
270
```typescript { .api }
271
interface TypeChecker {
272
/**
273
* Convert a type to a TypeNode AST representation
274
* @param type - Type to convert
275
* @param enclosingDeclaration - Context for the conversion
276
* @param flags - Conversion flags
277
* @returns TypeNode AST or undefined
278
*/
279
typeToTypeNode(
280
type: Type,
281
enclosingDeclaration?: Node,
282
flags?: NodeBuilderFlags
283
): TypeNode | undefined;
284
285
/**
286
* Convert a symbol to EntityName AST representation
287
* @param symbol - Symbol to convert
288
* @param meaning - Symbol meaning context
289
* @param enclosingDeclaration - Context for conversion
290
* @returns EntityName AST or undefined
291
*/
292
symbolToEntityName(
293
symbol: Symbol,
294
meaning: SymbolFlags,
295
enclosingDeclaration?: Node
296
): EntityName | undefined;
297
298
/**
299
* Convert type to string representation
300
* @param type - Type to convert
301
* @param enclosingDeclaration - Context for conversion
302
* @param flags - Type formatting flags
303
* @returns String representation
304
*/
305
typeToString(
306
type: Type,
307
enclosingDeclaration?: Node,
308
flags?: TypeFormatFlags
309
): string;
310
311
/**
312
* Convert symbol to string representation
313
* @param symbol - Symbol to convert
314
* @param enclosingDeclaration - Context for conversion
315
* @param meaning - Symbol meaning
316
* @returns String representation
317
*/
318
symbolToString(
319
symbol: Symbol,
320
enclosingDeclaration?: Node,
321
meaning?: SymbolFlags
322
): string;
323
}
324
```
325
326
### Diagnostics
327
328
Get semantic diagnostics and error information.
329
330
```typescript { .api }
331
interface TypeChecker {
332
/**
333
* Get semantic diagnostics for a source file
334
* @param sourceFile - Source file to analyze
335
* @returns Array of diagnostics
336
*/
337
getSemanticDiagnostics(sourceFile?: SourceFile): readonly Diagnostic[];
338
339
/**
340
* Get suggestion diagnostics (hints, not errors)
341
* @param sourceFile - Source file to analyze
342
* @returns Array of suggestion diagnostics
343
*/
344
getSuggestionDiagnostics(sourceFile: SourceFile): readonly DiagnosticWithLocation[];
345
346
/**
347
* Get global diagnostics (not tied to specific files)
348
* @returns Array of global diagnostics
349
*/
350
getGlobalDiagnostics(): readonly Diagnostic[];
351
}
352
```
353
354
## Types
355
356
### Core Type System
357
358
```typescript { .api }
359
interface Type {
360
flags: TypeFlags;
361
symbol: Symbol;
362
pattern?: DestructuringPattern;
363
aliasSymbol?: Symbol;
364
aliasTypeArguments?: readonly Type[];
365
}
366
367
enum TypeFlags {
368
Any = 1,
369
Unknown = 2,
370
String = 4,
371
Number = 8,
372
Boolean = 16,
373
Enum = 32,
374
BigInt = 64,
375
StringLiteral = 128,
376
NumberLiteral = 256,
377
BooleanLiteral = 512,
378
EnumLiteral = 1024,
379
BigIntLiteral = 2048,
380
ESSymbol = 4096,
381
UniqueESSymbol = 8192,
382
Void = 16384,
383
Undefined = 32768,
384
Null = 65536,
385
Never = 131072,
386
TypeParameter = 262144,
387
Object = 524288,
388
Union = 1048576,
389
Intersection = 2097152,
390
Index = 4194304,
391
IndexedAccess = 8388608,
392
Conditional = 16777216,
393
Substitution = 33554432,
394
NonPrimitive = 67108864
395
}
396
397
interface Symbol {
398
flags: SymbolFlags;
399
escapedName: __String;
400
declarations: Declaration[];
401
valueDeclaration: Declaration;
402
members?: SymbolTable;
403
exports?: SymbolTable;
404
globalExports?: SymbolTable;
405
}
406
407
enum SymbolFlags {
408
None = 0,
409
FunctionScopedVariable = 1,
410
BlockScopedVariable = 2,
411
Property = 4,
412
EnumMember = 8,
413
Function = 16,
414
Class = 32,
415
Interface = 64,
416
ConstEnum = 128,
417
RegularEnum = 256,
418
ValueModule = 512,
419
NamespaceModule = 1024,
420
TypeLiteral = 2048,
421
ObjectLiteral = 4096,
422
Method = 8192,
423
Constructor = 16384,
424
GetAccessor = 32768,
425
SetAccessor = 65536,
426
Signature = 131072,
427
TypeParameter = 262144,
428
TypeAlias = 524288,
429
ExportValue = 1048576,
430
Alias = 2097152,
431
Prototype = 4194304,
432
ExportStar = 8388608,
433
Optional = 16777216,
434
Transient = 33554432
435
}
436
```
437
438
### Signature Types
439
440
```typescript { .api }
441
interface Signature {
442
declaration?: SignatureDeclaration;
443
typeParameters?: readonly TypeParameter[];
444
parameters: readonly Symbol[];
445
resolvedReturnType: Type;
446
minArgumentCount: number;
447
hasRestParameter: boolean;
448
hasLiteralTypes: boolean;
449
}
450
451
enum SignatureKind {
452
Call = 0,
453
Construct = 1
454
}
455
456
interface CallSignature extends Signature {
457
kind: SignatureKind.Call;
458
}
459
460
interface ConstructSignature extends Signature {
461
kind: SignatureKind.Construct;
462
}
463
```
464
465
### Index Types
466
467
```typescript { .api }
468
enum IndexKind {
469
String = 0,
470
Number = 1
471
}
472
473
interface IndexInfo {
474
type: Type;
475
isReadonly: boolean;
476
declaration?: IndexSignatureDeclaration;
477
}
478
```