Type validation functions provide runtime type checking for AST nodes. They include both type guard functions (returning boolean) and assertion functions (throwing errors for invalid types).
/**
* Checks if a value is any AST node
* @param node - Value to check
* @returns Type guard indicating if value is a Node
*/
function isNode(node: any): node is t.Node;
/**
* Generic type validation with optional property matching
* @param type - Expected node type string
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for the specified type
*/
function isType<T extends t.Node["type"]>(
type: T,
node: t.Node | null | undefined,
opts?: object
): node is Extract<t.Node, { type: T }>;
/**
* Main validation function with generic type support
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard with proper typing
*/
function is<T extends t.Node>(
type: T["type"],
node: t.Node | null | undefined,
opts?: Partial<T>
): node is T;/**
* Validates if node is an Identifier
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for Identifier
*/
function isIdentifier(
node: t.Node | null | undefined,
opts?: Partial<t.Identifier>
): node is t.Identifier;/**
* Validates if node is a StringLiteral
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for StringLiteral
*/
function isStringLiteral(
node: t.Node | null | undefined,
opts?: Partial<t.StringLiteral>
): node is t.StringLiteral;
/**
* Validates if node is a NumericLiteral
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for NumericLiteral
*/
function isNumericLiteral(
node: t.Node | null | undefined,
opts?: Partial<t.NumericLiteral>
): node is t.NumericLiteral;
/**
* Validates if node is a BooleanLiteral
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for BooleanLiteral
*/
function isBooleanLiteral(
node: t.Node | null | undefined,
opts?: Partial<t.BooleanLiteral>
): node is t.BooleanLiteral;
/**
* Validates if node is a NullLiteral
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for NullLiteral
*/
function isNullLiteral(
node: t.Node | null | undefined,
opts?: Partial<t.NullLiteral>
): node is t.NullLiteral;
/**
* Validates if node is a RegExpLiteral
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for RegExpLiteral
*/
function isRegExpLiteral(
node: t.Node | null | undefined,
opts?: Partial<t.RegExpLiteral>
): node is t.RegExpLiteral;
/**
* Validates if node is a BigIntLiteral
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for BigIntLiteral
*/
function isBigIntLiteral(
node: t.Node | null | undefined,
opts?: Partial<t.BigIntLiteral>
): node is t.BigIntLiteral;/**
* Validates if node is a BinaryExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for BinaryExpression
*/
function isBinaryExpression(
node: t.Node | null | undefined,
opts?: Partial<t.BinaryExpression>
): node is t.BinaryExpression;
/**
* Validates if node is an UnaryExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for UnaryExpression
*/
function isUnaryExpression(
node: t.Node | null | undefined,
opts?: Partial<t.UnaryExpression>
): node is t.UnaryExpression;
/**
* Validates if node is an AssignmentExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for AssignmentExpression
*/
function isAssignmentExpression(
node: t.Node | null | undefined,
opts?: Partial<t.AssignmentExpression>
): node is t.AssignmentExpression;
/**
* Validates if node is a CallExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for CallExpression
*/
function isCallExpression(
node: t.Node | null | undefined,
opts?: Partial<t.CallExpression>
): node is t.CallExpression;
/**
* Validates if node is a MemberExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for MemberExpression
*/
function isMemberExpression(
node: t.Node | null | undefined,
opts?: Partial<t.MemberExpression>
): node is t.MemberExpression;
/**
* Validates if node is a ConditionalExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ConditionalExpression
*/
function isConditionalExpression(
node: t.Node | null | undefined,
opts?: Partial<t.ConditionalExpression>
): node is t.ConditionalExpression;
/**
* Validates if node is an ArrayExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ArrayExpression
*/
function isArrayExpression(
node: t.Node | null | undefined,
opts?: Partial<t.ArrayExpression>
): node is t.ArrayExpression;
/**
* Validates if node is an ObjectExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ObjectExpression
*/
function isObjectExpression(
node: t.Node | null | undefined,
opts?: Partial<t.ObjectExpression>
): node is t.ObjectExpression;
/**
* Validates if node is a FunctionExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for FunctionExpression
*/
function isFunctionExpression(
node: t.Node | null | undefined,
opts?: Partial<t.FunctionExpression>
): node is t.FunctionExpression;
/**
* Validates if node is an ArrowFunctionExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ArrowFunctionExpression
*/
function isArrowFunctionExpression(
node: t.Node | null | undefined,
opts?: Partial<t.ArrowFunctionExpression>
): node is t.ArrowFunctionExpression;/**
* Validates if node is a BlockStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for BlockStatement
*/
function isBlockStatement(
node: t.Node | null | undefined,
opts?: Partial<t.BlockStatement>
): node is t.BlockStatement;
/**
* Validates if node is an ExpressionStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ExpressionStatement
*/
function isExpressionStatement(
node: t.Node | null | undefined,
opts?: Partial<t.ExpressionStatement>
): node is t.ExpressionStatement;
/**
* Validates if node is a ReturnStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ReturnStatement
*/
function isReturnStatement(
node: t.Node | null | undefined,
opts?: Partial<t.ReturnStatement>
): node is t.ReturnStatement;
/**
* Validates if node is a VariableDeclaration
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for VariableDeclaration
*/
function isVariableDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.VariableDeclaration>
): node is t.VariableDeclaration;
/**
* Validates if node is a FunctionDeclaration
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for FunctionDeclaration
*/
function isFunctionDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.FunctionDeclaration>
): node is t.FunctionDeclaration;/**
* Validates if node is an IfStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for IfStatement
*/
function isIfStatement(
node: t.Node | null | undefined,
opts?: Partial<t.IfStatement>
): node is t.IfStatement;
/**
* Validates if node is a WhileStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for WhileStatement
*/
function isWhileStatement(
node: t.Node | null | undefined,
opts?: Partial<t.WhileStatement>
): node is t.WhileStatement;
/**
* Validates if node is a ForStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ForStatement
*/
function isForStatement(
node: t.Node | null | undefined,
opts?: Partial<t.ForStatement>
): node is t.ForStatement;
/**
* Validates if node is a ForInStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ForInStatement
*/
function isForInStatement(
node: t.Node | null | undefined,
opts?: Partial<t.ForInStatement>
): node is t.ForInStatement;
/**
* Validates if node is a ForOfStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ForOfStatement
*/
function isForOfStatement(
node: t.Node | null | undefined,
opts?: Partial<t.ForOfStatement>
): node is t.ForOfStatement;
/**
* Validates if node is a SwitchStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for SwitchStatement
*/
function isSwitchStatement(
node: t.Node | null | undefined,
opts?: Partial<t.SwitchStatement>
): node is t.SwitchStatement;
/**
* Validates if node is a TryStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for TryStatement
*/
function isTryStatement(
node: t.Node | null | undefined,
opts?: Partial<t.TryStatement>
): node is t.TryStatement;
/**
* Validates if node is a ThrowStatement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ThrowStatement
*/
function isThrowStatement(
node: t.Node | null | undefined,
opts?: Partial<t.ThrowStatement>
): node is t.ThrowStatement;/**
* Validates if node is an ImportDeclaration
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ImportDeclaration
*/
function isImportDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.ImportDeclaration>
): node is t.ImportDeclaration;
/**
* Validates if node is an ExportNamedDeclaration
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ExportNamedDeclaration
*/
function isExportNamedDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.ExportNamedDeclaration>
): node is t.ExportNamedDeclaration;
/**
* Validates if node is an ExportDefaultDeclaration
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ExportDefaultDeclaration
*/
function isExportDefaultDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.ExportDefaultDeclaration>
): node is t.ExportDefaultDeclaration;/**
* Validates if node is a ClassDeclaration
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ClassDeclaration
*/
function isClassDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.ClassDeclaration>
): node is t.ClassDeclaration;
/**
* Validates if node is a ClassExpression
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ClassExpression
*/
function isClassExpression(
node: t.Node | null | undefined,
opts?: Partial<t.ClassExpression>
): node is t.ClassExpression;
/**
* Validates if node is a ClassMethod
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ClassMethod
*/
function isClassMethod(
node: t.Node | null | undefined,
opts?: Partial<t.ClassMethod>
): node is t.ClassMethod;
/**
* Validates if node is a ClassProperty
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for ClassProperty
*/
function isClassProperty(
node: t.Node | null | undefined,
opts?: Partial<t.ClassProperty>
): node is t.ClassProperty;/**
* Validates if node is a JSXElement
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for JSXElement
*/
function isJSXElement(
node: t.Node | null | undefined,
opts?: Partial<t.JSXElement>
): node is t.JSXElement;
/**
* Validates if node is a JSXIdentifier
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for JSXIdentifier
*/
function isJSXIdentifier(
node: t.Node | null | undefined,
opts?: Partial<t.JSXIdentifier>
): node is t.JSXIdentifier;
/**
* Validates if node is a JSXAttribute
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for JSXAttribute
*/
function isJSXAttribute(
node: t.Node | null | undefined,
opts?: Partial<t.JSXAttribute>
): node is t.JSXAttribute;/**
* Validates if node is a TSTypeAnnotation
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for TSTypeAnnotation
*/
function isTSTypeAnnotation(
node: t.Node | null | undefined,
opts?: Partial<t.TSTypeAnnotation>
): node is t.TSTypeAnnotation;
/**
* Validates if node is a TSInterfaceDeclaration
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for TSInterfaceDeclaration
*/
function isTSInterfaceDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.TSInterfaceDeclaration>
): node is t.TSInterfaceDeclaration;
/**
* Validates if node is a TSTypeAliasDeclaration
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for TSTypeAliasDeclaration
*/
function isTSTypeAliasDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.TSTypeAliasDeclaration>
): node is t.TSTypeAliasDeclaration;/**
* Validates if node is a TypeAnnotation
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for TypeAnnotation
*/
function isTypeAnnotation(
node: t.Node | null | undefined,
opts?: Partial<t.TypeAnnotation>
): node is t.TypeAnnotation;
/**
* Validates if node is a FlowType
* @param node - Node to validate
* @param opts - Optional properties to match
* @returns Type guard for FlowType
*/
function isFlowType(
node: t.Node | null | undefined,
opts?: Partial<t.FlowType>
): node is t.FlowType;Assertion functions throw errors when validation fails instead of returning boolean values. They provide TypeScript assertion signatures for type narrowing.
/**
* Asserts that node is any AST node, throws if not
* @param node - Value to assert
* @throws TypeError if not a valid node
*/
function assertNode(node: any): asserts node is t.Node;
/**
* Asserts that node is an Identifier, throws if not
* @param node - Node to assert
* @param opts - Optional properties to match
* @throws TypeError if not an Identifier
*/
function assertIdentifier(
node: t.Node | null | undefined,
opts?: Partial<t.Identifier>
): asserts node is t.Identifier;
/**
* Asserts that node is a StringLiteral, throws if not
* @param node - Node to assert
* @param opts - Optional properties to match
* @throws TypeError if not a StringLiteral
*/
function assertStringLiteral(
node: t.Node | null | undefined,
opts?: Partial<t.StringLiteral>
): asserts node is t.StringLiteral;
/**
* Asserts that node is a BinaryExpression, throws if not
* @param node - Node to assert
* @param opts - Optional properties to match
* @throws TypeError if not a BinaryExpression
*/
function assertBinaryExpression(
node: t.Node | null | undefined,
opts?: Partial<t.BinaryExpression>
): asserts node is t.BinaryExpression;
/**
* Asserts that node is a FunctionDeclaration, throws if not
* @param node - Node to assert
* @param opts - Optional properties to match
* @throws TypeError if not a FunctionDeclaration
*/
function assertFunctionDeclaration(
node: t.Node | null | undefined,
opts?: Partial<t.FunctionDeclaration>
): asserts node is t.FunctionDeclaration;/**
* Checks if identifier is a binding in the current scope
* @param node - Node to check (typically Identifier)
* @param name - Binding name to check for
* @returns Whether the identifier is a binding
*/
function isBinding(node: t.Node, name: string): boolean;
/**
* Checks if declaration creates block-scoped bindings
* @param node - Declaration node to check
* @returns Whether creates block-scoped bindings (let/const/class/function)
*/
function isBlockScoped(node: t.Node): node is t.FunctionDeclaration | t.ClassDeclaration | t.VariableDeclaration;
/**
* Checks if declaration is a let declaration
* @param node - Node to check
* @returns Whether node is let VariableDeclaration
*/
function isLet(node: t.Node): node is t.VariableDeclaration;
/**
* Checks if declaration is a var declaration
* @param node - Node to check
* @returns Whether node is var VariableDeclaration
*/
function isVar(node: t.Node): node is t.VariableDeclaration;
/**
* Checks if node represents a scope boundary
* @param node - Node to check
* @returns Whether node creates a new scope
*/
function isScope(node: t.Node): boolean;
/**
* Checks if identifier is referenced (not just declared)
* @param node - Identifier node to check
* @param parent - Parent node
* @returns Whether identifier is referenced
*/
function isReferenced(node: t.Identifier, parent: t.Node): boolean;/**
* Checks if node represents an immutable value
* @param node - Node to check
* @returns Whether the node represents immutable value
*/
function isImmutable(node: t.Node): boolean;
/**
* Checks if string is a valid ES3 identifier
* @param name - String to validate
* @returns Whether string is valid ES3 identifier
*/
function isValidES3Identifier(name: string): boolean;
/**
* Checks if string is a valid JavaScript identifier
* @param name - String to validate
* @returns Whether string is valid identifier
*/
function isValidIdentifier(name: string): boolean;
/**
* Checks if import/export specifier is default
* @param specifier - Specifier node to check
* @returns Whether specifier represents default import/export
*/
function isSpecifierDefault(specifier: t.ModuleSpecifier): boolean;/**
* Checks if node matches a member expression pattern
* @param node - Node to check (typically MemberExpression)
* @param match - Pattern to match against (e.g., "console.log")
* @returns Whether node matches the pattern
*/
function matchesPattern(
node: t.Node | null | undefined,
match: string | string[]
): boolean;
/**
* Creates a function to match member expression patterns
* @param pattern - Pattern to create matcher for
* @returns Function that checks if node matches pattern
*/
function buildMatchMemberExpression(
pattern: string
): (node: t.Node | null | undefined) => boolean;
/**
* Checks if two nodes are equivalent (structurally equal)
* @param left - First node to compare
* @param right - Second node to compare
* @returns Whether nodes are structurally equivalent
*/
function isNodesEquivalent(
left: t.Node | null | undefined,
right: t.Node | null | undefined
): boolean;
/**
* Checks if node is a placeholder type
* @param node - Node to check
* @param expectedNode - Expected placeholder type
* @returns Whether node is specified placeholder type
*/
function isPlaceholderType(
node: t.Node | null | undefined,
expectedNode: string
): boolean;Low-level validation function used internally by builders and assertions.
/**
* Validate a node property against field specifications
* @param node - Node being validated
* @param key - Property key being validated
* @param val - Value being validated
* @throws Error if validation fails
*/
function validate(node: t.Node | null | undefined, key: string, val: unknown): void;import * as t from "@babel/types";
// Type validation
const node = t.identifier("myVar");
if (t.isIdentifier(node)) {
console.log(node.name); // TypeScript knows this is safe
}
// Assertion with error handling
try {
t.assertBinaryExpression(node); // Throws TypeError
} catch (error) {
console.log("Not a binary expression");
}
// Validation with property matching
const strLiteral = t.stringLiteral("hello");
if (t.isStringLiteral(strLiteral, { value: "hello" })) {
console.log("Matches expected value");
}
// Utility validations
const letDecl = t.variableDeclaration("let", [
t.variableDeclarator(t.identifier("x"))
]);
console.log(t.isLet(letDecl)); // true
console.log(t.isBlockScoped(letDecl)); // true
console.log(t.isValidIdentifier("myVar")); // true
console.log(t.isValidIdentifier("123abc")); // false
// Pattern matching
const memberExpr = t.memberExpression(
t.identifier("console"),
t.identifier("log")
);
console.log(t.matchesPattern(memberExpr, "console.log")); // true
// Node equivalence
const node1 = t.identifier("test");
const node2 = t.identifier("test");
console.log(t.isNodesEquivalent(node1, node2)); // trueBabel Types provides 306+ automatically generated assertion functions that throw errors when validation fails. These functions are essential for type safety and debugging in AST manipulation code.
/**
* Assert that a value is any AST node, throws if invalid
* @param node - Value to assert
* @param opts - Optional properties to match
* @throws Error if node is not valid
*/
function assertNode(node: unknown, opts?: object): asserts node is t.Node;// Array and Object Expressions
function assertArrayExpression(node: object | null | undefined, opts?: object | null): asserts node is t.ArrayExpression;
function assertObjectExpression(node: object | null | undefined, opts?: object | null): asserts node is t.ObjectExpression;
function assertObjectMethod(node: object | null | undefined, opts?: object | null): asserts node is t.ObjectMethod;
function assertObjectProperty(node: object | null | undefined, opts?: object | null): asserts node is t.ObjectProperty;
// Binary and Unary Expressions
function assertBinaryExpression(node: object | null | undefined, opts?: object | null): asserts node is t.BinaryExpression;
function assertUnaryExpression(node: object | null | undefined, opts?: object | null): asserts node is t.UnaryExpression;
function assertUpdateExpression(node: object | null | undefined, opts?: object | null): asserts node is t.UpdateExpression;
function assertLogicalExpression(node: object | null | undefined, opts?: object | null): asserts node is t.LogicalExpression;
// Assignment and Call Expressions
function assertAssignmentExpression(node: object | null | undefined, opts?: object | null): asserts node is t.AssignmentExpression;
function assertCallExpression(node: object | null | undefined, opts?: object | null): asserts node is t.CallExpression;
function assertNewExpression(node: object | null | undefined, opts?: object | null): asserts node is t.NewExpression;
function assertMemberExpression(node: object | null | undefined, opts?: object | null): asserts node is t.MemberExpression;
// Conditional and Sequence Expressions
function assertConditionalExpression(node: object | null | undefined, opts?: object | null): asserts node is t.ConditionalExpression;
function assertSequenceExpression(node: object | null | undefined, opts?: object | null): asserts node is t.SequenceExpression;
function assertParenthesizedExpression(node: object | null | undefined, opts?: object | null): asserts node is t.ParenthesizedExpression;
// Function Expressions
function assertFunctionExpression(node: object | null | undefined, opts?: object | null): asserts node is t.FunctionExpression;
function assertArrowFunctionExpression(node: object | null | undefined, opts?: object | null): asserts node is t.ArrowFunctionExpression;
// ES6+ Expressions
function assertYieldExpression(node: object | null | undefined, opts?: object | null): asserts node is t.YieldExpression;
function assertAwaitExpression(node: object | null | undefined, opts?: object | null): asserts node is t.AwaitExpression;
function assertSpreadElement(node: object | null | undefined, opts?: object | null): asserts node is t.SpreadElement;
function assertTemplateLiteral(node: object | null | undefined, opts?: object | null): asserts node is t.TemplateLiteral;
function assertTaggedTemplateExpression(node: object | null | undefined, opts?: object | null): asserts node is t.TaggedTemplateExpression;
// Class Expressions
function assertClassExpression(node: object | null | undefined, opts?: object | null): asserts node is t.ClassExpression;
function assertSuper(node: object | null | undefined, opts?: object | null): asserts node is t.Super;
function assertThisExpression(node: object | null | undefined, opts?: object | null): asserts node is t.ThisExpression;// Block and Control Flow Statements
function assertBlockStatement(node: object | null | undefined, opts?: object | null): asserts node is t.BlockStatement;
function assertExpressionStatement(node: object | null | undefined, opts?: object | null): asserts node is t.ExpressionStatement;
function assertEmptyStatement(node: object | null | undefined, opts?: object | null): asserts node is t.EmptyStatement;
function assertDebuggerStatement(node: object | null | undefined, opts?: object | null): asserts node is t.DebuggerStatement;
// Control Flow Statements
function assertIfStatement(node: object | null | undefined, opts?: object | null): asserts node is t.IfStatement;
function assertSwitchStatement(node: object | null | undefined, opts?: object | null): asserts node is t.SwitchStatement;
function assertSwitchCase(node: object | null | undefined, opts?: object | null): asserts node is t.SwitchCase;
function assertWhileStatement(node: object | null | undefined, opts?: object | null): asserts node is t.WhileStatement;
function assertDoWhileStatement(node: object | null | undefined, opts?: object | null): asserts node is t.DoWhileStatement;
function assertForStatement(node: object | null | undefined, opts?: object | null): asserts node is t.ForStatement;
function assertForInStatement(node: object | null | undefined, opts?: object | null): asserts node is t.ForInStatement;
function assertForOfStatement(node: object | null | undefined, opts?: object | null): asserts node is t.ForOfStatement;
// Jump Statements
function assertBreakStatement(node: object | null | undefined, opts?: object | null): asserts node is t.BreakStatement;
function assertContinueStatement(node: object | null | undefined, opts?: object | null): asserts node is t.ContinueStatement;
function assertReturnStatement(node: object | null | undefined, opts?: object | null): asserts node is t.ReturnStatement;
function assertThrowStatement(node: object | null | undefined, opts?: object | null): asserts node is t.ThrowStatement;
// Exception Handling
function assertTryStatement(node: object | null | undefined, opts?: object | null): asserts node is t.TryStatement;
function assertCatchClause(node: object | null | undefined, opts?: object | null): asserts node is t.CatchClause;
// Labels and Directives
function assertLabeledStatement(node: object | null | undefined, opts?: object | null): asserts node is t.LabeledStatement;
function assertWithStatement(node: object | null | undefined, opts?: object | null): asserts node is t.WithStatement;
function assertDirective(node: object | null | undefined, opts?: object | null): asserts node is t.Directive;
function assertDirectiveLiteral(node: object | null | undefined, opts?: object | null): asserts node is t.DirectiveLiteral;// Function and Variable Declarations
function assertFunctionDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.FunctionDeclaration;
function assertVariableDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.VariableDeclaration;
function assertVariableDeclarator(node: object | null | undefined, opts?: object | null): asserts node is t.VariableDeclarator;
// Class Declarations
function assertClassDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.ClassDeclaration;
function assertClassMethod(node: object | null | undefined, opts?: object | null): asserts node is t.ClassMethod;
function assertClassProperty(node: object | null | undefined, opts?: object | null): asserts node is t.ClassProperty;
function assertMethodDefinition(node: object | null | undefined, opts?: object | null): asserts node is t.MethodDefinition;
// Import/Export Declarations
function assertImportDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.ImportDeclaration;
function assertImportSpecifier(node: object | null | undefined, opts?: object | null): asserts node is t.ImportSpecifier;
function assertImportDefaultSpecifier(node: object | null | undefined, opts?: object | null): asserts node is t.ImportDefaultSpecifier;
function assertImportNamespaceSpecifier(node: object | null | undefined, opts?: object | null): asserts node is t.ImportNamespaceSpecifier;
function assertExportNamedDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.ExportNamedDeclaration;
function assertExportDefaultDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.ExportDefaultDeclaration;
function assertExportAllDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.ExportAllDeclaration;
function assertExportSpecifier(node: object | null | undefined, opts?: object | null): asserts node is t.ExportSpecifier;// Primitive Literals
function assertStringLiteral(node: object | null | undefined, opts?: object | null): asserts node is t.StringLiteral;
function assertNumericLiteral(node: object | null | undefined, opts?: object | null): asserts node is t.NumericLiteral;
function assertBooleanLiteral(node: object | null | undefined, opts?: object | null): asserts node is t.BooleanLiteral;
function assertNullLiteral(node: object | null | undefined, opts?: object | null): asserts node is t.NullLiteral;
function assertRegExpLiteral(node: object | null | undefined, opts?: object | null): asserts node is t.RegExpLiteral;
function assertBigIntLiteral(node: object | null | undefined, opts?: object | null): asserts node is t.BigIntLiteral;
// Template Literals
function assertTemplateElement(node: object | null | undefined, opts?: object | null): asserts node is t.TemplateElement;// Identifiers
function assertIdentifier(node: object | null | undefined, opts?: object | null): asserts node is t.Identifier;
function assertPrivateName(node: object | null | undefined, opts?: object | null): asserts node is t.PrivateName;
// Patterns
function assertArrayPattern(node: object | null | undefined, opts?: object | null): asserts node is t.ArrayPattern;
function assertObjectPattern(node: object | null | undefined, opts?: object | null): asserts node is t.ObjectPattern;
function assertRestElement(node: object | null | undefined, opts?: object | null): asserts node is t.RestElement;
function assertAssignmentPattern(node: object | null | undefined, opts?: object | null): asserts node is t.AssignmentPattern;// JSX Elements
function assertJSXElement(node: object | null | undefined, opts?: object | null): asserts node is t.JSXElement;
function assertJSXOpeningElement(node: object | null | undefined, opts?: object | null): asserts node is t.JSXOpeningElement;
function assertJSXClosingElement(node: object | null | undefined, opts?: object | null): asserts node is t.JSXClosingElement;
function assertJSXFragment(node: object | null | undefined, opts?: object | null): asserts node is t.JSXFragment;
function assertJSXText(node: object | null | undefined, opts?: object | null): asserts node is t.JSXText;
// JSX Attributes and Expressions
function assertJSXAttribute(node: object | null | undefined, opts?: object | null): asserts node is t.JSXAttribute;
function assertJSXSpreadAttribute(node: object | null | undefined, opts?: object | null): asserts node is t.JSXSpreadAttribute;
function assertJSXExpressionContainer(node: object | null | undefined, opts?: object | null): asserts node is t.JSXExpressionContainer;
function assertJSXIdentifier(node: object | null | undefined, opts?: object | null): asserts node is t.JSXIdentifier;
function assertJSXMemberExpression(node: object | null | undefined, opts?: object | null): asserts node is t.JSXMemberExpression;
function assertJSXNamespacedName(node: object | null | undefined, opts?: object | null): asserts node is t.JSXNamespacedName;// TypeScript Type Annotations
function assertTSTypeAnnotation(node: object | null | undefined, opts?: object | null): asserts node is t.TSTypeAnnotation;
function assertTSTypeParameterInstantiation(node: object | null | undefined, opts?: object | null): asserts node is t.TSTypeParameterInstantiation;
function assertTSTypeParameterDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.TSTypeParameterDeclaration;
function assertTSTypeParameter(node: object | null | undefined, opts?: object | null): asserts node is t.TSTypeParameter;
// TypeScript Types
function assertTSStringKeyword(node: object | null | undefined, opts?: object | null): asserts node is t.TSStringKeyword;
function assertTSNumberKeyword(node: object | null | undefined, opts?: object | null): asserts node is t.TSNumberKeyword;
function assertTSBooleanKeyword(node: object | null | undefined, opts?: object | null): asserts node is t.TSBooleanKeyword;
function assertTSUnionType(node: object | null | undefined, opts?: object | null): asserts node is t.TSUnionType;
function assertTSIntersectionType(node: object | null | undefined, opts?: object | null): asserts node is t.TSIntersectionType;
// TypeScript Declarations
function assertTSTypeAliasDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.TSTypeAliasDeclaration;
function assertTSInterfaceDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.TSInterfaceDeclaration;
function assertTSEnumDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.TSEnumDeclaration;
function assertTSModuleDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.TSModuleDeclaration;// Flow Type Annotations
function assertTypeAnnotation(node: object | null | undefined, opts?: object | null): asserts node is t.TypeAnnotation;
function assertTypeParameterInstantiation(node: object | null | undefined, opts?: object | null): asserts node is t.TypeParameterInstantiation;
function assertTypeParameterDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.TypeParameterDeclaration;
function assertTypeParameter(node: object | null | undefined, opts?: object | null): asserts node is t.TypeParameter;
// Flow Types
function assertStringTypeAnnotation(node: object | null | undefined, opts?: object | null): asserts node is t.StringTypeAnnotation;
function assertNumberTypeAnnotation(node: object | null | undefined, opts?: object | null): asserts node is t.NumberTypeAnnotation;
function assertBooleanTypeAnnotation(node: object | null | undefined, opts?: object | null): asserts node is t.BooleanTypeAnnotation;
function assertUnionTypeAnnotation(node: object | null | undefined, opts?: object | null): asserts node is t.UnionTypeAnnotation;
function assertIntersectionTypeAnnotation(node: object | null | undefined, opts?: object | null): asserts node is t.IntersectionTypeAnnotation;
// Flow Declarations
function assertTypeAlias(node: object | null | undefined, opts?: object | null): asserts node is t.TypeAlias;
function assertInterfaceDeclaration(node: object | null | undefined, opts?: object | null): asserts node is t.InterfaceDeclaration;
function assertDeclareFunction(node: object | null | undefined, opts?: object | null): asserts node is t.DeclareFunction;
function assertDeclareVariable(node: object | null | undefined, opts?: object | null): asserts node is t.DeclareVariable;import * as t from "@babel/types";
const node = t.identifier("myVar");
// These assertions will pass
t.assertIdentifier(node);
t.assertNode(node);
// This assertion will throw an error
try {
t.assertStringLiteral(node);
} catch (error) {
console.log(error.message); // Expected type "StringLiteral" but got "Identifier"
}const binaryExpr = t.binaryExpression("+", t.identifier("a"), t.identifier("b"));
// Assert with specific operator
t.assertBinaryExpression(binaryExpr, { operator: "+" }); // Passes
// This will throw because operator doesn't match
try {
t.assertBinaryExpression(binaryExpr, { operator: "-" });
} catch (error) {
console.log("Operator mismatch detected");
}function processExpression(node: t.Node) {
// Ensure we have the right node type before processing
t.assertBinaryExpression(node);
// Now TypeScript knows node is definitely a BinaryExpression
console.log(`Processing ${node.operator} operation`);
// Process left and right operands safely
t.assertExpression(node.left);
t.assertExpression(node.right);
}function doubleNumericLiterals(ast: t.Node) {
t.traverse(ast, (node, ancestors, state) => {
if (t.isNumericLiteral(node)) {
// Double-check with assertion for safety
t.assertNumericLiteral(node);
// Safe to access .value property
node.value = node.value * 2;
}
});
}function validateFunctionCall(node: t.Node) {
try {
t.assertCallExpression(node);
t.assertIdentifier(node.callee);
console.log(`Valid function call to: ${node.callee.name}`);
return true;
} catch (error) {
console.log(`Invalid function call: ${error.message}`);
return false;
}
}Note: All assertion functions throw a descriptive error when validation fails, making them ideal for debugging and ensuring type safety in AST transformation code. The complete list includes 306+ generated assertion functions covering every AST node type supported by Babel.