API reference/AST types

AST types

@swc-next/types provides TypeScript declarations for the AST, including TypeScript, JSX, and error recovery nodes. These types are also re-exported by the core, parser, codegen, and browser packages.

import type { Node, Program, Lang, SourceType } from "@swc-next/types";

Program and nodes

interface BaseNode {
  start: number;
  end: number;
}

interface Program extends BaseNode {
  type: "Program";
  body: Array<Directive | Statement>;
  hashbang: Hashbang | null;
  sourceType: SourceType;
}

Node is the union of supported node declarations, discriminated by type. Positions use UTF-16 code units with exclusive end offsets. Source positions on synthetic nodes are the caller's responsibility.

Language and source type

type Lang = "js" | "ts" | "jsx" | "tsx" | "dts";
type SourceType = "module" | "script" | "commonjs" | "unambiguous";

These are type aliases, not runtime enums. Use string values with the Parser API.

Compatibility

The JavaScript API produces ASTs compatible with the ESTree specification, using typescript-eslint's AST format for TypeScript and JSX nodes. The exported declarations describe these node shapes, including error recovery nodes.

Set the parser's preserveParens option to false to match typescript-eslint's representation of parentheses. The default preserves explicit parenthesized expression and type nodes.