Comprehensive TypeScript type definitions for the SWC JavaScript/TypeScript transformation and minification APIs.
—
Complete abstract syntax tree type definitions covering all JavaScript and TypeScript syntax elements. These types represent the parsed structure of source code and are essential for AST manipulation, analysis, and transformation.
Base interfaces that form the foundation of the AST type system.
/**
* Base AST node interface
*/
interface Node {
type: string;
}
/**
* Nodes with source location information
*/
interface HasSpan {
span: Span;
}
/**
* Nodes that can have decorators
*/
interface HasDecorator {
decorators?: Decorator[];
}
/**
* Source location span
*/
interface Span {
start: number;
end: number;
ctxt: number;
}Top-level program types representing complete source files.
/**
* Top-level program - either a module or script
*/
type Program = Module | Script;
/**
* ES Module program
*/
interface Module extends Node, HasSpan, HasInterpreter {
type: "Module";
body: ModuleItem[];
}
/**
* Script program
*/
interface Script extends Node, HasSpan, HasInterpreter {
type: "Script";
body: Statement[];
}
interface HasInterpreter {
/** e.g. "/usr/bin/node" for "#!/usr/bin/node" */
interpreter: string;
}
type ModuleItem = ModuleDeclaration | Statement;All statement types in JavaScript and TypeScript.
type Statement =
| BlockStatement | EmptyStatement | DebuggerStatement
| WithStatement | ReturnStatement | LabeledStatement
| BreakStatement | ContinueStatement | IfStatement
| SwitchStatement | ThrowStatement | TryStatement
| WhileStatement | DoWhileStatement | ForStatement
| ForInStatement | ForOfStatement | Declaration
| ExpressionStatement;
interface BlockStatement extends Node, HasSpan {
type: "BlockStatement";
stmts: Statement[];
}
interface ExpressionStatement extends Node, HasSpan {
type: "ExpressionStatement";
expression: Expression;
}
interface EmptyStatement extends Node, HasSpan {
type: "EmptyStatement";
}
interface IfStatement extends Node, HasSpan {
type: "IfStatement";
test: Expression;
consequent: Statement;
alternate?: Statement;
}
interface WhileStatement extends Node, HasSpan {
type: "WhileStatement";
test: Expression;
body: Statement;
}
interface ForStatement extends Node, HasSpan {
type: "ForStatement";
init?: VariableDeclaration | Expression;
test?: Expression;
update?: Expression;
body: Statement;
}
interface ReturnStatement extends Node, HasSpan {
type: "ReturnStatement";
argument?: Expression;
}
interface TryStatement extends Node, HasSpan {
type: "TryStatement";
block: BlockStatement;
handler?: CatchClause;
finalizer?: BlockStatement;
}
interface CatchClause extends Node, HasSpan {
type: "CatchClause";
param?: Pattern;
body: BlockStatement;
}All expression types in JavaScript and TypeScript.
type Expression =
| ThisExpression | ArrayExpression | ObjectExpression
| FunctionExpression | UnaryExpression | UpdateExpression
| BinaryExpression | AssignmentExpression | MemberExpression
| SuperPropExpression | ConditionalExpression | CallExpression
| NewExpression | SequenceExpression | Identifier | Literal
| TemplateLiteral | TaggedTemplateExpression | ArrowFunctionExpression
| ClassExpression | YieldExpression | MetaProperty | AwaitExpression
| ParenthesisExpression | JSXMemberExpression | JSXNamespacedName
| JSXEmptyExpression | JSXElement | JSXFragment | TsTypeAssertion
| TsConstAssertion | TsNonNullExpression | TsAsExpression
| TsSatisfiesExpression | TsInstantiation | PrivateName
| OptionalChainingExpression | Invalid;
interface Identifier extends ExpressionBase {
type: "Identifier";
value: string;
optional: boolean;
}
interface BinaryExpression extends ExpressionBase {
type: "BinaryExpression";
operator: BinaryOperator;
left: Expression;
right: Expression;
}
interface CallExpression extends ExpressionBase {
type: "CallExpression";
callee: Super | Import | Expression;
arguments: Argument[];
typeArguments?: TsTypeParameterInstantiation;
}
interface MemberExpression extends ExpressionBase {
type: "MemberExpression";
object: Expression;
property: Identifier | PrivateName | ComputedPropName;
}
interface ArrowFunctionExpression extends ExpressionBase {
type: "ArrowFunctionExpression";
params: Pattern[];
body: BlockStatement | Expression;
async: boolean;
generator: boolean;
typeParameters?: TsTypeParameterDeclaration;
returnType?: TsTypeAnnotation;
}Declaration statement types including functions, classes, and variables.
type Declaration =
| ClassDeclaration | FunctionDeclaration | VariableDeclaration
| TsInterfaceDeclaration | TsTypeAliasDeclaration | TsEnumDeclaration
| TsModuleDeclaration;
interface FunctionDeclaration extends Fn {
type: "FunctionDeclaration";
identifier: Identifier;
declare: boolean;
}
interface ClassDeclaration extends Class, Node {
type: "ClassDeclaration";
identifier: Identifier;
declare: boolean;
}
interface VariableDeclaration extends Node, HasSpan {
type: "VariableDeclaration";
kind: VariableDeclarationKind;
declare: boolean;
declarations: VariableDeclarator[];
}
type VariableDeclarationKind = "var" | "let" | "const";
interface VariableDeclarator extends Node, HasSpan {
type: "VariableDeclarator";
id: Pattern;
init?: Expression;
definite: boolean;
}Class-related AST nodes including members, methods, and properties.
interface Class extends HasSpan, HasDecorator {
body: ClassMember[];
superClass?: Expression;
isAbstract: boolean;
typeParams?: TsTypeParameterDeclaration;
superTypeParams?: TsTypeParameterInstantiation;
implements: TsExpressionWithTypeArguments[];
}
type ClassMember =
| Constructor | ClassMethod | PrivateMethod
| ClassProperty | PrivateProperty | TsIndexSignature
| EmptyStatement | StaticBlock;
interface ClassProperty extends ClassPropertyBase {
type: "ClassProperty";
key: PropertyName;
isAbstract: boolean;
declare: boolean;
}
interface ClassMethod extends ClassMethodBase {
type: "ClassMethod";
key: PropertyName;
}
interface Constructor extends Node, HasSpan {
type: "Constructor";
key: PropertyName;
params: (TsParameterProperty | Param)[];
body?: BlockStatement;
accessibility?: Accessibility;
isOptional: boolean;
}
interface StaticBlock extends Node, HasSpan {
type: "StaticBlock";
body: BlockStatement;
}
type Accessibility = "public" | "protected" | "private";
type MethodKind = "method" | "getter" | "setter";Pattern types used for destructuring assignments and function parameters.
type Pattern =
| BindingIdentifier | ArrayPattern | RestElement
| ObjectPattern | AssignmentPattern | Invalid | Expression;
interface BindingIdentifier extends PatternBase {
type: "Identifier";
value: string;
optional: boolean;
}
interface ArrayPattern extends PatternBase {
type: "ArrayPattern";
elements: (Pattern | undefined)[];
optional: boolean;
}
interface ObjectPattern extends PatternBase {
type: "ObjectPattern";
properties: ObjectPatternProperty[];
optional: boolean;
}
interface AssignmentPattern extends PatternBase {
type: "AssignmentPattern";
left: Pattern;
right: Expression;
}
interface RestElement extends PatternBase {
type: "RestElement";
rest: Span;
argument: Pattern;
}
type ObjectPatternProperty =
| KeyValuePatternProperty | AssignmentPatternProperty | RestElement;Literal value types in JavaScript.
type Literal =
| StringLiteral | BooleanLiteral | NullLiteral
| NumericLiteral | BigIntLiteral | RegExpLiteral | JSXText;
interface StringLiteral extends Node, HasSpan {
type: "StringLiteral";
value: string;
raw?: string;
}
interface NumericLiteral extends Node, HasSpan {
type: "NumericLiteral";
value: number;
raw?: string;
}
interface BooleanLiteral extends Node, HasSpan {
type: "BooleanLiteral";
value: boolean;
}
interface BigIntLiteral extends Node, HasSpan {
type: "BigIntLiteral";
value: bigint;
raw?: string;
}
interface RegExpLiteral extends Node, HasSpan {
type: "RegExpLiteral";
pattern: string;
flags: string;
}Module import/export declarations and specifiers.
type ModuleDeclaration =
| ImportDeclaration | ExportDeclaration | ExportNamedDeclaration
| ExportDefaultDeclaration | ExportDefaultExpression | ExportAllDeclaration
| TsImportEqualsDeclaration | TsExportAssignment | TsNamespaceExportDeclaration;
interface ImportDeclaration extends Node, HasSpan {
type: "ImportDeclaration";
specifiers: ImportSpecifier[];
source: StringLiteral;
typeOnly: boolean;
asserts?: ObjectExpression;
}
interface ExportNamedDeclaration extends Node, HasSpan {
type: "ExportNamedDeclaration";
specifiers: ExportSpecifier[];
source?: StringLiteral;
typeOnly: boolean;
asserts?: ObjectExpression;
}
type ImportSpecifier =
| NamedImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier;
type ExportSpecifier =
| ExportNamespaceSpecifier | ExportDefaultSpecifier | NamedExportSpecifier;
interface NamedImportSpecifier extends Node, HasSpan {
type: "ImportSpecifier";
local: Identifier;
imported?: ModuleExportName;
isTypeOnly: boolean;
}
type ModuleExportName = Identifier | StringLiteral;Operator type definitions for expressions.
type BinaryOperator =
| "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">="
| "<<" | ">>" | ">>>" | "+" | "-" | "*" | "/" | "%"
| "|" | "^" | "&" | "||" | "&&" | "in" | "instanceof" | "**" | "??";
type AssignmentOperator =
| "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "<<=" | ">>=" | ">>>="
| "|=" | "^=" | "&=" | "**=" | "&&=" | "||=" | "??=";
type UpdateOperator = "++" | "--";
type UnaryOperator = "-" | "+" | "!" | "~" | "typeof" | "void" | "delete";Supporting interfaces used throughout the AST.
interface ExpressionBase extends Node, HasSpan { }
interface PatternBase extends Node, HasSpan {
typeAnnotation?: TsTypeAnnotation;
}
interface Fn extends HasSpan, HasDecorator {
params: Param[];
body?: BlockStatement;
generator: boolean;
async: boolean;
typeParameters?: TsTypeParameterDeclaration;
returnType?: TsTypeAnnotation;
}
interface Param extends Node, HasSpan, HasDecorator {
type: "Parameter";
pat: Pattern;
}
interface Decorator extends Node, HasSpan {
type: "Decorator";
expression: Expression;
}
interface Invalid extends Node, HasSpan {
type: "Invalid";
}
interface Output {
code: string;
map?: string;
}
/**
* Expression or spread element for object properties and array elements
*/
interface ExprOrSpread {
spread?: Span;
expression: Expression;
}
/**
* Function call argument with optional spread
*/
interface Argument {
spread?: Span;
expression: Expression;
}
/**
* Spread element for arrays, objects, and function calls
*/
interface SpreadElement extends Node {
type: "SpreadElement";
spread: Span;
arguments: Expression;
}
/**
* Computed property name for dynamic object keys
*/
interface ComputedPropName {
type: "ComputedPropName";
expression: Expression;
}
/**
* Pattern matching interface (empty placeholder)
*/
interface MatchPattern { }Usage Examples:
import type { Program, Statement, Expression, Identifier } from "@swc/types";
// Type-safe AST traversal
function findIdentifiers(node: Program): Identifier[] {
const identifiers: Identifier[] = [];
function visit(node: any) {
if (node && typeof node === 'object') {
if (node.type === 'Identifier') {
identifiers.push(node as Identifier);
}
for (const key in node) {
const child = node[key];
if (Array.isArray(child)) {
child.forEach(visit);
} else if (child && typeof child === 'object') {
visit(child);
}
}
}
}
visit(node);
return identifiers;
}
// AST node creation
function createStringLiteral(value: string): StringLiteral {
return {
type: "StringLiteral",
span: { start: 0, end: 0, ctxt: 0 },
value,
raw: `"${value}"`
};
}Install with Tessl CLI
npx tessl i tessl/npm-swc--types