0
# TypeScript ESTree
1
2
Complete type definitions for all AST node structures with parent relationship augmentations, providing strongly-typed access to the entire TypeScript and JavaScript syntax tree.
3
4
## Capabilities
5
6
### TSESTree Namespace
7
8
The complete TypeScript ESTree namespace containing all AST node type definitions.
9
10
```typescript { .api }
11
declare namespace TSESTree {
12
// Core data interface (from ast-spec)
13
interface NodeOrTokenData {
14
type: string;
15
loc: SourceLocation;
16
range: Range;
17
}
18
19
// Base node interface with parent relationship
20
interface BaseNode extends NodeOrTokenData {
21
type: AST_NODE_TYPES;
22
parent: Node; // Added by @typescript-eslint/types package
23
}
24
25
// Program node (root of AST)
26
interface Program extends NodeOrTokenData {
27
type: AST_NODE_TYPES.Program;
28
body: ProgramStatement[];
29
comments: Comment[] | undefined;
30
sourceType: 'module' | 'script';
31
tokens: Token[] | undefined;
32
parent?: never; // Program has no parent
33
}
34
35
// Union type of all possible nodes
36
type Node = Program | Statement | Expression | Declaration | /* ... all node types */;
37
}
38
```
39
40
### Core Type Definitions
41
42
Essential type definitions used throughout the AST.
43
44
```typescript { .api }
45
/**
46
* Source range information - array of two numbers representing start and end positions
47
* Both numbers are 0-based indices in the source code character array
48
*/
49
type Range = [number, number];
50
51
/**
52
* Source location information
53
*/
54
interface SourceLocation {
55
start: Position;
56
end: Position;
57
}
58
59
/**
60
* Position in source code
61
*/
62
interface Position {
63
/** Line number (1-indexed) */
64
line: number;
65
/** Column number on the line (0-indexed) */
66
column: number;
67
}
68
69
/**
70
* Comment node
71
*/
72
interface Comment extends NodeOrTokenData {
73
type: 'Block' | 'Line';
74
value: string;
75
}
76
```
77
78
### Statement Nodes
79
80
Type definitions for all statement node types.
81
82
```typescript { .api }
83
// Variable declarations
84
interface VariableDeclaration extends BaseNode {
85
type: AST_NODE_TYPES.VariableDeclaration;
86
declarations: VariableDeclarator[];
87
kind: 'var' | 'let' | 'const' | 'using' | 'await using';
88
}
89
90
interface VariableDeclarator extends BaseNode {
91
type: AST_NODE_TYPES.VariableDeclarator;
92
id: BindingName;
93
init: Expression | null;
94
definite?: boolean;
95
}
96
97
// Function declarations
98
interface FunctionDeclaration extends BaseNode {
99
type: AST_NODE_TYPES.FunctionDeclaration;
100
id: Identifier | null;
101
params: Parameter[];
102
body: BlockStatement;
103
async: boolean;
104
generator: boolean;
105
declare?: boolean;
106
expression: false;
107
returnType?: TSTypeAnnotation;
108
typeParameters?: TSTypeParameterDeclaration;
109
}
110
111
// Control flow statements
112
interface IfStatement extends BaseNode {
113
type: AST_NODE_TYPES.IfStatement;
114
test: Expression;
115
consequent: Statement;
116
alternate: Statement | null;
117
}
118
119
interface ForStatement extends BaseNode {
120
type: AST_NODE_TYPES.ForStatement;
121
init: VariableDeclaration | Expression | null;
122
test: Expression | null;
123
update: Expression | null;
124
body: Statement;
125
}
126
```
127
128
### Expression Nodes
129
130
Type definitions for expression node types.
131
132
```typescript { .api }
133
// Function expressions
134
interface ArrowFunctionExpression extends BaseNode {
135
type: AST_NODE_TYPES.ArrowFunctionExpression;
136
id: null;
137
params: Parameter[];
138
body: BlockStatement | Expression;
139
async: boolean;
140
generator: false;
141
expression: boolean;
142
returnType?: TSTypeAnnotation;
143
typeParameters?: TSTypeParameterDeclaration;
144
}
145
146
// Member access
147
interface MemberExpression extends BaseNode {
148
type: AST_NODE_TYPES.MemberExpression;
149
object: Expression;
150
property: Expression;
151
computed: boolean;
152
optional?: boolean;
153
}
154
155
// Function calls
156
interface CallExpression extends BaseNode {
157
type: AST_NODE_TYPES.CallExpression;
158
callee: Expression;
159
arguments: CallExpressionArgument[];
160
optional?: boolean;
161
typeArguments?: TSTypeParameterInstantiation;
162
}
163
164
// Binary operations
165
interface BinaryExpression extends BaseNode {
166
type: AST_NODE_TYPES.BinaryExpression;
167
left: Expression;
168
operator: BinaryOperator;
169
right: Expression;
170
}
171
172
type BinaryOperator =
173
| '==' | '!=' | '===' | '!=='
174
| '<' | '<=' | '>' | '>='
175
| '<<' | '>>' | '>>>'
176
| '+' | '-' | '*' | '/' | '%' | '**'
177
| '|' | '^' | '&'
178
| 'in' | 'instanceof';
179
```
180
181
### TypeScript-Specific Nodes
182
183
Type definitions for TypeScript language extensions.
184
185
```typescript { .api }
186
// Type annotations
187
interface TSTypeAnnotation extends BaseNode {
188
type: AST_NODE_TYPES.TSTypeAnnotation;
189
typeAnnotation: TypeNode;
190
}
191
192
// Interface declarations
193
interface TSInterfaceDeclaration extends BaseNode {
194
type: AST_NODE_TYPES.TSInterfaceDeclaration;
195
id: Identifier;
196
body: TSInterfaceBody;
197
extends?: TSInterfaceHeritage[];
198
typeParameters?: TSTypeParameterDeclaration;
199
abstract?: boolean;
200
declare?: boolean;
201
}
202
203
interface TSInterfaceBody extends BaseNode {
204
type: AST_NODE_TYPES.TSInterfaceBody;
205
body: TypeElement[];
206
}
207
208
// Type alias declarations
209
interface TSTypeAliasDeclaration extends BaseNode {
210
type: AST_NODE_TYPES.TSTypeAliasDeclaration;
211
id: Identifier;
212
typeAnnotation: TypeNode;
213
typeParameters?: TSTypeParameterDeclaration;
214
declare?: boolean;
215
}
216
217
// Enum declarations
218
interface TSEnumDeclaration extends BaseNode {
219
type: AST_NODE_TYPES.TSEnumDeclaration;
220
id: Identifier;
221
body: TSEnumBody;
222
const?: boolean;
223
declare?: boolean;
224
}
225
226
// Type expressions
227
interface TSAsExpression extends BaseNode {
228
type: AST_NODE_TYPES.TSAsExpression;
229
expression: Expression;
230
typeAnnotation: TypeNode;
231
}
232
233
interface TSNonNullExpression extends BaseNode {
234
type: AST_NODE_TYPES.TSNonNullExpression;
235
expression: Expression;
236
}
237
```
238
239
### Union Types
240
241
Common union types used throughout the AST.
242
243
```typescript { .api }
244
// All possible nodes
245
type Node =
246
| Program
247
| Statement
248
| Expression
249
| Declaration
250
| TypeNode
251
| Comment;
252
253
// Statements
254
type Statement =
255
| BlockStatement
256
| VariableDeclaration
257
| FunctionDeclaration
258
| IfStatement
259
| ForStatement
260
| WhileStatement
261
| DoWhileStatement
262
| ReturnStatement
263
| BreakStatement
264
| ContinueStatement
265
| ThrowStatement
266
| TryStatement
267
| ExpressionStatement
268
/* ... all statement types */;
269
270
// Expressions
271
type Expression =
272
| Identifier
273
| Literal
274
| ArrowFunctionExpression
275
| FunctionExpression
276
| CallExpression
277
| MemberExpression
278
| BinaryExpression
279
| UnaryExpression
280
| UpdateExpression
281
| ConditionalExpression
282
/* ... all expression types */;
283
284
// TypeScript type nodes
285
type TypeNode =
286
| TSAnyKeyword
287
| TSStringKeyword
288
| TSNumberKeyword
289
| TSBooleanKeyword
290
| TSArrayType
291
| TSUnionType
292
| TSIntersectionType
293
| TSTypeReference
294
/* ... all type nodes */;
295
```
296
297
**Usage Examples:**
298
299
```typescript
300
import { TSESTree, AST_NODE_TYPES } from "@typescript-eslint/types";
301
302
// Type-safe AST traversal
303
function traverseNode(node: TSESTree.Node): void {
304
switch (node.type) {
305
case AST_NODE_TYPES.FunctionDeclaration:
306
// TypeScript knows this is a FunctionDeclaration
307
console.log(`Function: ${node.id?.name}`);
308
if (node.returnType) {
309
console.log('Has return type annotation');
310
}
311
break;
312
313
case AST_NODE_TYPES.TSInterfaceDeclaration:
314
// TypeScript knows this is a TSInterfaceDeclaration
315
console.log(`Interface: ${node.id.name}`);
316
node.body.body.forEach(member => {
317
// Process interface members
318
});
319
break;
320
321
case AST_NODE_TYPES.CallExpression:
322
// TypeScript knows this is a CallExpression
323
if (node.typeArguments) {
324
console.log('Generic function call');
325
}
326
break;
327
}
328
}
329
330
// Parent relationship access
331
function getParentFunction(node: TSESTree.Node): TSESTree.FunctionDeclaration | null {
332
let current = node.parent;
333
while (current) {
334
if (current.type === AST_NODE_TYPES.FunctionDeclaration) {
335
return current; // TypeScript knows this is FunctionDeclaration
336
}
337
current = current.parent;
338
}
339
return null;
340
}
341
342
// Type predicate functions
343
function isFunctionLike(node: TSESTree.Node): node is TSESTree.FunctionLike {
344
return [
345
AST_NODE_TYPES.FunctionDeclaration,
346
AST_NODE_TYPES.FunctionExpression,
347
AST_NODE_TYPES.ArrowFunctionExpression,
348
AST_NODE_TYPES.MethodDefinition
349
].includes(node.type);
350
}
351
352
function isTypeScriptNode(node: TSESTree.Node): boolean {
353
return node.type.startsWith('TS');
354
}
355
356
// Extract information from nodes
357
function extractFunctionInfo(node: TSESTree.FunctionDeclaration) {
358
return {
359
name: node.id?.name || '<anonymous>',
360
paramCount: node.params.length,
361
isAsync: node.async,
362
isGenerator: node.generator,
363
hasTypeParameters: !!node.typeParameters,
364
hasReturnType: !!node.returnType,
365
isDeclared: !!node.declare
366
};
367
}
368
```
369
370
## Parent Relationships
371
372
The TypeScript ESTree types include augmented parent relationships for all nodes:
373
374
### BaseNode Parent Property
375
376
Every AST node (except Program) has a strongly-typed `parent` property:
377
378
```typescript
379
interface BaseNode {
380
parent: TSESTree.Node; // Points to the parent node
381
}
382
383
interface Program {
384
parent?: never; // Program has no parent
385
}
386
```
387
388
### Specific Parent Relationships
389
390
Many nodes have specific parent type constraints:
391
392
```typescript
393
interface CatchClause {
394
parent: TSESTree.TryStatement; // Always child of TryStatement
395
}
396
397
interface SwitchCase {
398
parent: TSESTree.SwitchStatement; // Always child of SwitchStatement
399
}
400
401
interface TSEnumMember {
402
parent: TSESTree.TSEnumBody; // Always child of TSEnumBody
403
}
404
```
405
406
This enables type-safe parent access without runtime checks or type assertions.