0
# TypeScript Support
1
2
TypeScript-specific AST nodes and type system representations for TypeScript compilation and analysis. These types cover the complete TypeScript syntax including type annotations, generics, interfaces, and advanced type constructs.
3
4
## Capabilities
5
6
### Type System
7
8
Core TypeScript type representations covering all type expressions.
9
10
```typescript { .api }
11
/**
12
* All TypeScript type expressions
13
*/
14
type TsType =
15
| TsKeywordType | TsThisType | TsFnOrConstructorType
16
| TsTypeReference | TsTypeQuery | TsTypeLiteral
17
| TsArrayType | TsTupleType | TsOptionalType
18
| TsRestType | TsUnionOrIntersectionType
19
| TsConditionalType | TsInferType | TsParenthesizedType
20
| TsTypeOperator | TsIndexedAccessType | TsMappedType
21
| TsLiteralType | TsTypePredicate | TsImportType;
22
23
/**
24
* Primitive TypeScript keyword types
25
*/
26
interface TsKeywordType extends Node, HasSpan {
27
type: "TsKeywordType";
28
kind: TsKeywordTypeKind;
29
}
30
31
type TsKeywordTypeKind =
32
| "any" | "unknown" | "number" | "object" | "boolean"
33
| "bigint" | "string" | "symbol" | "void" | "undefined"
34
| "null" | "never" | "intrinsic";
35
36
/**
37
* 'this' type reference
38
*/
39
interface TsThisType extends Node, HasSpan {
40
type: "TsThisType";
41
}
42
43
/**
44
* Type reference (e.g., Array<T>, MyInterface)
45
*/
46
interface TsTypeReference extends Node, HasSpan {
47
type: "TsTypeReference";
48
typeName: TsEntityName;
49
typeParams?: TsTypeParameterInstantiation;
50
}
51
52
/**
53
* Union and intersection types
54
*/
55
type TsUnionOrIntersectionType = TsUnionType | TsIntersectionType;
56
57
interface TsUnionType extends Node, HasSpan {
58
type: "TsUnionType";
59
types: TsType[];
60
}
61
62
interface TsIntersectionType extends Node, HasSpan {
63
type: "TsIntersectionType";
64
types: TsType[];
65
}
66
```
67
68
### Function and Constructor Types
69
70
Type definitions for callable types.
71
72
```typescript { .api }
73
type TsFnOrConstructorType = TsFunctionType | TsConstructorType;
74
75
/**
76
* Function type signature
77
*/
78
interface TsFunctionType extends Node, HasSpan {
79
type: "TsFunctionType";
80
params: TsFnParameter[];
81
typeParams?: TsTypeParameterDeclaration;
82
typeAnnotation: TsTypeAnnotation;
83
}
84
85
/**
86
* Constructor type signature
87
*/
88
interface TsConstructorType extends Node, HasSpan {
89
type: "TsConstructorType";
90
params: TsFnParameter[];
91
typeParams?: TsTypeParameterDeclaration;
92
typeAnnotation: TsTypeAnnotation;
93
isAbstract: boolean;
94
}
95
96
type TsFnParameter =
97
| BindingIdentifier | ArrayPattern | RestElement | ObjectPattern;
98
```
99
100
### Complex Type Constructs
101
102
Advanced TypeScript type system features.
103
104
```typescript { .api }
105
/**
106
* Array type (T[])
107
*/
108
interface TsArrayType extends Node, HasSpan {
109
type: "TsArrayType";
110
elemType: TsType;
111
}
112
113
/**
114
* Tuple type ([T, U, V])
115
*/
116
interface TsTupleType extends Node, HasSpan {
117
type: "TsTupleType";
118
elemTypes: TsTupleElement[];
119
}
120
121
interface TsTupleElement extends Node, HasSpan {
122
type: "TsTupleElement";
123
label?: Pattern;
124
ty: TsType;
125
}
126
127
/**
128
* Conditional type (T extends U ? X : Y)
129
*/
130
interface TsConditionalType extends Node, HasSpan {
131
type: "TsConditionalType";
132
checkType: TsType;
133
extendsType: TsType;
134
trueType: TsType;
135
falseType: TsType;
136
}
137
138
/**
139
* Mapped type ({ [K in T]: U })
140
*/
141
interface TsMappedType extends Node, HasSpan {
142
type: "TsMappedType";
143
readonly?: TruePlusMinus;
144
typeParam: TsTypeParameter;
145
nameType?: TsType;
146
optional?: TruePlusMinus;
147
typeAnnotation?: TsType;
148
}
149
150
type TruePlusMinus = true | "+" | "-";
151
152
/**
153
* Indexed access type (T[K])
154
*/
155
interface TsIndexedAccessType extends Node, HasSpan {
156
type: "TsIndexedAccessType";
157
readonly: boolean;
158
objectType: TsType;
159
indexType: TsType;
160
}
161
162
/**
163
* Type operator (keyof T, readonly T, unique T)
164
*/
165
interface TsTypeOperator extends Node, HasSpan {
166
type: "TsTypeOperator";
167
op: TsTypeOperatorOp;
168
typeAnnotation: TsType;
169
}
170
171
type TsTypeOperatorOp = "keyof" | "unique" | "readonly";
172
```
173
174
### Type Declarations
175
176
TypeScript declaration types including interfaces, type aliases, and enums.
177
178
```typescript { .api }
179
/**
180
* Interface declaration
181
*/
182
interface TsInterfaceDeclaration extends Node, HasSpan {
183
type: "TsInterfaceDeclaration";
184
id: Identifier;
185
declare: boolean;
186
typeParams?: TsTypeParameterDeclaration;
187
extends: TsExpressionWithTypeArguments[];
188
body: TsInterfaceBody;
189
}
190
191
interface TsInterfaceBody extends Node, HasSpan {
192
type: "TsInterfaceBody";
193
body: TsTypeElement[];
194
}
195
196
/**
197
* Type alias declaration
198
*/
199
interface TsTypeAliasDeclaration extends Node, HasSpan {
200
type: "TsTypeAliasDeclaration";
201
declare: boolean;
202
id: Identifier;
203
typeParams?: TsTypeParameterDeclaration;
204
typeAnnotation: TsType;
205
}
206
207
/**
208
* Enum declaration
209
*/
210
interface TsEnumDeclaration extends Node, HasSpan {
211
type: "TsEnumDeclaration";
212
declare: boolean;
213
isConst: boolean;
214
id: Identifier;
215
members: TsEnumMember[];
216
}
217
218
interface TsEnumMember extends Node, HasSpan {
219
type: "TsEnumMember";
220
id: TsEnumMemberId;
221
init?: Expression;
222
}
223
224
type TsEnumMemberId = Identifier | StringLiteral;
225
```
226
227
### Type Elements and Signatures
228
229
Type elements used in interfaces and type literals.
230
231
```typescript { .api }
232
type TsTypeElement =
233
| TsCallSignatureDeclaration | TsConstructSignatureDeclaration
234
| TsPropertySignature | TsGetterSignature | TsSetterSignature
235
| TsMethodSignature | TsIndexSignature;
236
237
/**
238
* Call signature in interface
239
*/
240
interface TsCallSignatureDeclaration extends Node, HasSpan {
241
type: "TsCallSignatureDeclaration";
242
params: TsFnParameter[];
243
typeAnnotation?: TsTypeAnnotation;
244
typeParams?: TsTypeParameterDeclaration;
245
}
246
247
/**
248
* Constructor signature in interface
249
*/
250
interface TsConstructSignatureDeclaration extends Node, HasSpan {
251
type: "TsConstructSignatureDeclaration";
252
params: TsFnParameter[];
253
typeAnnotation?: TsTypeAnnotation;
254
typeParams?: TsTypeParameterDeclaration;
255
}
256
257
/**
258
* Property signature in interface
259
*/
260
interface TsPropertySignature extends Node, HasSpan {
261
type: "TsPropertySignature";
262
readonly: boolean;
263
key: Expression;
264
computed: boolean;
265
optional: boolean;
266
typeAnnotation?: TsTypeAnnotation;
267
}
268
269
/**
270
* Method signature in interface
271
*/
272
interface TsMethodSignature extends Node, HasSpan {
273
type: "TsMethodSignature";
274
readonly: boolean;
275
key: Expression;
276
computed: boolean;
277
optional: boolean;
278
params: TsFnParameter[];
279
typeAnnotation?: TsTypeAnnotation;
280
typeParams?: TsTypeParameterDeclaration;
281
}
282
283
/**
284
* Index signature in interface
285
*/
286
interface TsIndexSignature extends Node, HasSpan {
287
type: "TsIndexSignature";
288
params: TsFnParameter[];
289
typeAnnotation?: TsTypeAnnotation;
290
readonly: boolean;
291
static: boolean;
292
}
293
```
294
295
### Type Parameters and Generics
296
297
Generic type parameter definitions and instantiations.
298
299
```typescript { .api }
300
/**
301
* Type parameter declaration (<T, U>)
302
*/
303
interface TsTypeParameterDeclaration extends Node, HasSpan {
304
type: "TsTypeParameterDeclaration";
305
parameters: TsTypeParameter[];
306
}
307
308
/**
309
* Individual type parameter
310
*/
311
interface TsTypeParameter extends Node, HasSpan {
312
type: "TsTypeParameter";
313
name: Identifier;
314
in: boolean;
315
out: boolean;
316
constraint?: TsType;
317
default?: TsType;
318
}
319
320
/**
321
* Type argument instantiation (<string, number>)
322
*/
323
interface TsTypeParameterInstantiation extends Node, HasSpan {
324
type: "TsTypeParameterInstantiation";
325
params: TsType[];
326
}
327
```
328
329
### Type Annotations and Assertions
330
331
Type annotations and type assertion expressions.
332
333
```typescript { .api }
334
/**
335
* Type annotation (: Type)
336
*/
337
interface TsTypeAnnotation extends Node, HasSpan {
338
type: "TsTypeAnnotation";
339
typeAnnotation: TsType;
340
}
341
342
/**
343
* Type assertion (expr as Type)
344
*/
345
interface TsAsExpression extends ExpressionBase {
346
type: "TsAsExpression";
347
expression: Expression;
348
typeAnnotation: TsType;
349
}
350
351
/**
352
* Satisfies expression (expr satisfies Type)
353
*/
354
interface TsSatisfiesExpression extends ExpressionBase {
355
type: "TsSatisfiesExpression";
356
expression: Expression;
357
typeAnnotation: TsType;
358
}
359
360
/**
361
* Type assertion (<Type>expr)
362
*/
363
interface TsTypeAssertion extends ExpressionBase {
364
type: "TsTypeAssertion";
365
expression: Expression;
366
typeAnnotation: TsType;
367
}
368
369
/**
370
* Const assertion (expr as const)
371
*/
372
interface TsConstAssertion extends ExpressionBase {
373
type: "TsConstAssertion";
374
expression: Expression;
375
}
376
377
/**
378
* Non-null assertion (expr!)
379
*/
380
interface TsNonNullExpression extends ExpressionBase {
381
type: "TsNonNullExpression";
382
expression: Expression;
383
}
384
```
385
386
### Module and Namespace Declarations
387
388
TypeScript module and namespace syntax.
389
390
```typescript { .api }
391
/**
392
* Module/namespace declaration
393
*/
394
interface TsModuleDeclaration extends Node, HasSpan {
395
type: "TsModuleDeclaration";
396
declare: boolean;
397
global: boolean;
398
id: TsModuleName;
399
body?: TsNamespaceBody;
400
}
401
402
/**
403
* Namespace declaration body
404
*/
405
type TsNamespaceBody = TsModuleBlock | TsNamespaceDeclaration;
406
407
interface TsModuleBlock extends Node, HasSpan {
408
type: "TsModuleBlock";
409
body: ModuleItem[];
410
}
411
412
interface TsNamespaceDeclaration extends Node, HasSpan {
413
type: "TsNamespaceDeclaration";
414
declare: boolean;
415
global: boolean;
416
id: Identifier;
417
body: TsNamespaceBody;
418
}
419
420
type TsModuleName = Identifier | StringLiteral;
421
422
/**
423
* Import equals declaration
424
*/
425
interface TsImportEqualsDeclaration extends Node, HasSpan {
426
type: "TsImportEqualsDeclaration";
427
declare: boolean;
428
isExport: boolean;
429
isTypeOnly: boolean;
430
id: Identifier;
431
moduleRef: TsModuleReference;
432
}
433
434
type TsModuleReference = TsEntityName | TsExternalModuleReference;
435
436
interface TsExternalModuleReference extends Node, HasSpan {
437
type: "TsExternalModuleReference";
438
expression: StringLiteral;
439
}
440
```
441
442
### Utility Types and Helpers
443
444
Supporting types and utility interfaces for TypeScript features.
445
446
```typescript { .api }
447
type TsEntityName = TsQualifiedName | Identifier;
448
449
interface TsQualifiedName extends Node {
450
type: "TsQualifiedName";
451
left: TsEntityName;
452
right: Identifier;
453
}
454
455
/**
456
* Type literal ({ key: Type })
457
*/
458
interface TsTypeLiteral extends Node, HasSpan {
459
type: "TsTypeLiteral";
460
members: TsTypeElement[];
461
}
462
463
/**
464
* Literal type ("string" | 42 | true)
465
*/
466
interface TsLiteralType extends Node, HasSpan {
467
type: "TsLiteralType";
468
literal: TsLiteral;
469
}
470
471
type TsLiteral =
472
| NumericLiteral | StringLiteral | BooleanLiteral
473
| BigIntLiteral | TsTemplateLiteralType;
474
475
/**
476
* Template literal type (`hello ${T}`)
477
*/
478
interface TsTemplateLiteralType extends Node, HasSpan {
479
type: "TemplateLiteral";
480
types: TsType[];
481
quasis: TemplateElement[];
482
}
483
484
/**
485
* Type predicate (arg is Type)
486
*/
487
interface TsTypePredicate extends Node, HasSpan {
488
type: "TsTypePredicate";
489
asserts: boolean;
490
paramName: TsThisTypeOrIdent;
491
typeAnnotation?: TsTypeAnnotation;
492
}
493
494
type TsThisTypeOrIdent = TsThisType | Identifier;
495
496
/**
497
* Import type (import("module").Type)
498
*/
499
interface TsImportType extends Node, HasSpan {
500
type: "TsImportType";
501
argument: StringLiteral;
502
qualifier?: TsEntityName;
503
typeArguments?: TsTypeParameterInstantiation;
504
}
505
506
/**
507
* Parameter property (constructor(public prop: Type))
508
*/
509
interface TsParameterProperty extends Node, HasSpan, HasDecorator {
510
type: "TsParameterProperty";
511
accessibility?: Accessibility;
512
override: boolean;
513
readonly: boolean;
514
param: TsParameterPropertyParameter;
515
}
516
517
type TsParameterPropertyParameter = BindingIdentifier | AssignmentPattern;
518
```
519
520
**Usage Examples:**
521
522
```typescript
523
import type {
524
TsType, TsInterfaceDeclaration, TsTypeAliasDeclaration,
525
TsTypeAnnotation, TsAsExpression
526
} from "@swc/types";
527
528
// Type-safe TypeScript AST manipulation
529
function extractInterfaceProperties(
530
decl: TsInterfaceDeclaration
531
): Array<{ name: string; type: TsType }> {
532
return decl.body.body
533
.filter(member => member.type === "TsPropertySignature")
534
.map(member => ({
535
name: member.key.type === "Identifier" ? member.key.value : "",
536
type: member.typeAnnotation?.typeAnnotation
537
}))
538
.filter(prop => prop.type !== undefined);
539
}
540
541
// Create type annotation
542
function createTypeAnnotation(tsType: TsType): TsTypeAnnotation {
543
return {
544
type: "TsTypeAnnotation",
545
span: { start: 0, end: 0, ctxt: 0 },
546
typeAnnotation: tsType
547
};
548
}
549
550
// Check if expression has type assertion
551
function hasTypeAssertion(expr: Expression): expr is TsAsExpression {
552
return expr.type === "TsAsExpression";
553
}
554
```