A quick reference of all available TypeScript AST nodes.
##Types
##Primitives
| Node |
Output |
new TypeScriptString() |
string |
new TypeScriptNumber() |
number |
new TypeScriptBoolean() |
boolean |
new TypeScriptNull() |
null |
new TypeScriptUndefined() |
undefined |
new TypeScriptVoid() |
void |
new TypeScriptNever() |
never |
new TypeScriptUnknown() |
unknown |
new TypeScriptAny() |
any |
##Combining Types
TypeScriptUnion — string | number
new TypeScriptUnion([new TypeScriptString(), new TypeScriptNumber()])
TypeScriptIntersection — A & B
new TypeScriptIntersection([new TypeScriptIdentifier('A'), new TypeScriptIdentifier('B')])
TypeScriptArray — string[]
new TypeScriptArray([new TypeScriptString()])
TypeScriptTuple — [string, number]
new TypeScriptTuple([new TypeScriptString(), new TypeScriptNumber()])
##Generics
TypeScriptGeneric — Record<string, number>
Used both for generic type usage (concrete type arguments) and generic type declarations (with TypeScriptGenericTypeParameter arguments).
new TypeScriptGeneric(new TypeScriptIdentifier('Record'), [new TypeScriptString(), new TypeScriptNumber()])
new TypeScriptGeneric(new TypeScriptIdentifier('Container'), [
new TypeScriptGenericTypeParameter(new TypeScriptIdentifier('T'), extends: new TypeScriptIdentifier('object')),
])
TypeScriptGenericTypeParameter — T, T extends string, T extends string = string
Declares a generic type variable with optional constraint and default. Used inside TypeScriptGeneric for type declarations.
new TypeScriptGenericTypeParameter(new TypeScriptIdentifier('T'))
new TypeScriptGenericTypeParameter(new TypeScriptIdentifier('T'), extends: new TypeScriptString())
new TypeScriptGenericTypeParameter(new TypeScriptIdentifier('T'), extends: new TypeScriptString(), default: new TypeScriptString())
##Advanced Type Operators
TypeScriptConditional — T extends string ? number : boolean
new TypeScriptConditional(
TypeScriptOperator::extends(new TypeScriptIdentifier('T'), new TypeScriptString()),
new TypeScriptNumber(),
new TypeScriptBoolean(),
)
TypeScriptMappedType — { [K in keyof T]: T[K] }
new TypeScriptMappedType(
'K',
TypeScriptOperator::keyof(new TypeScriptIdentifier('T')),
new TypeScriptIndexedAccess(new TypeScriptIdentifier('T'), [new TypeScriptIdentifier('K')]),
)
new TypeScriptMappedType(
'K',
TypeScriptOperator::keyof(new TypeScriptIdentifier('T')),
new TypeScriptIndexedAccess(new TypeScriptIdentifier('T'), [new TypeScriptIdentifier('K')]),
readonlyModifier: 'readonly',
optionalModifier: '?',
)
TypeScriptIndexedAccess — User["name"]
new TypeScriptIndexedAccess(new TypeScriptIdentifier('User'), [new TypeScriptLiteral('name')])
TypeScriptOperator — keyof T, typeof config, T extends U
TypeScriptOperator::keyof(new TypeScriptIdentifier('T'))
TypeScriptOperator::typeof(new TypeScriptIdentifier('config'))
TypeScriptOperator::extends(new TypeScriptIdentifier('T'), new TypeScriptIdentifier('U'))
TypeScriptCallable — (...args: any[]) => any or custom function types like (x: string) => void
new TypeScriptCallable()
new TypeScriptCallable([new TypeScriptParameter('x', new TypeScriptString())], new TypeScriptVoid())
##Objects & Interfaces
TypeScriptObject — { name: string }
For describing object type shapes in type annotations. Compare with TypeScriptObjectLiteral for value-level JSON objects.
new TypeScriptObject([new TypeScriptProperty('name', new TypeScriptString())])
TypeScriptProperty — readonly name?: string
new TypeScriptProperty('name', new TypeScriptString(), isOptional: true, isReadonly: true)
TypeScriptIndexSignature — [key: string]
new TypeScriptIndexSignature(new TypeScriptString(), 'key')
TypeScriptInterface — interface User { name: string; greet(): void; }
new TypeScriptInterface('User', [new TypeScriptProperty('name', new TypeScriptString())], [new TypeScriptMethodSignature('greet', [], new TypeScriptVoid())])
TypeScriptMethodSignature — getName(id: number): string;
new TypeScriptMethodSignature('getName', [new TypeScriptParameter('id', new TypeScriptNumber())], new TypeScriptString())
##Declarations & Expressions
##Declarations
TypeScriptAlias — type Name = string;
new TypeScriptAlias('Name', new TypeScriptString())
TypeScriptEnum — enum Status { Active = 'active' }
new TypeScriptEnum('Status', [['name' => 'Active', 'value' => 'active']])
TypeScriptFunctionDeclaration — function greet(name: string): string { ... }
new TypeScriptFunctionDeclaration('greet', [new TypeScriptParameter('name', new TypeScriptString())], new TypeScriptString(), new TypeScriptRaw('return name;'))
TypeScriptVariableDeclaration — const name = "world"
TypeScriptVariableDeclaration::const('name', new TypeScriptLiteral('world'))
TypeScriptOperator::export() — export type Name = string;
TypeScriptOperator::export(new TypeScriptAlias('Name', new TypeScriptString()))
TypeScriptImport — import { User as AppUser } from './types';
new TypeScriptImport('./types', [['name' => 'User', 'alias' => 'AppUser']])
TypeScriptNamespace — declare namespace App { namespace Models { ... } } or namespace Models { ... }
new TypeScriptNamespace('App', [$typeNode], children: [
new TypeScriptNamespace('Models', [$otherTypeNode], declare: false)
])
##Expressions
Value-level nodes that produce JavaScript/TypeScript expressions. Some output similar syntax to type-level nodes but serve a different purpose.
TypeScriptCallExpression — createAction<UserParams>("index")
new TypeScriptCallExpression(new TypeScriptIdentifier('createAction'), [new TypeScriptLiteral('index')], genericTypes: [new TypeScriptIdentifier('UserParams')])
TypeScriptArrayExpression — ["a", "b", "c"]
For array literals in expressions. Compare with TypeScriptTuple for type-level tuples.
new TypeScriptArrayExpression([new TypeScriptLiteral('a'), new TypeScriptLiteral('b'), new TypeScriptLiteral('c')])
TypeScriptObjectLiteral — { "method": "GET", "url": "/users" }
For JSON object values. Compare with TypeScriptObject for type-level object shapes.
new TypeScriptObjectLiteral(['method' => 'GET', 'url' => '/users'])
##Building Blocks
Low-level nodes used as parts of other nodes.
TypeScriptIdentifier — MyType (auto-quotes invalid identifiers)
new TypeScriptIdentifier('MyType')
TypeScriptLiteral — "hello", 42, true
new TypeScriptLiteral('hello')
TypeScriptParameter — name?: string, ...args: string[]
new TypeScriptParameter('name', new TypeScriptString(), isOptional: true)
TypeScriptRaw — pass-through raw TypeScript, supports references for %placeholder% substitution and additionalImports for external TS file imports
new TypeScriptRaw('Record<string, never>')
new TypeScriptRaw('%User% | null', references: ['User' => UserData::class])