Utility functions for working with TypeScript's Abstract Syntax Tree (AST) and type system
—
Type guard functions for TypeScript AST nodes using ts.SyntaxKind enumeration. These functions provide type-safe narrowing of ts.Node union types to specific node types, enabling safe access to type-specific properties and methods.
Core node type checking for fundamental AST constructs.
/**
* Check if node is an identifier
* @param node - AST node to check
* @returns true if node is ts.Identifier
*/
function isIdentifier(node: ts.Node): node is ts.Identifier;
/**
* Check if node is a source file
* @param node - AST node to check
* @returns true if node is ts.SourceFile
*/
function isSourceFile(node: ts.Node): node is ts.SourceFile;
/**
* Check if node is a block statement
* @param node - AST node to check
* @returns true if node is ts.Block
*/
function isBlock(node: ts.Node): node is ts.Block;
/**
* Check if node is an expression
* @param node - AST node to check
* @returns true if node is ts.Expression
*/
function isExpression(node: ts.Node): node is ts.Expression;Type guards for all literal value types in TypeScript.
/**
* Check if node is a string literal
* @param node - AST node to check
* @returns true if node is ts.StringLiteral
*/
function isStringLiteral(node: ts.Node): node is ts.StringLiteral;
/**
* Check if node is a numeric literal
* @param node - AST node to check
* @returns true if node is ts.NumericLiteral
*/
function isNumericLiteral(node: ts.Node): node is ts.NumericLiteral;
/**
* Check if node is a boolean literal (true/false)
* @param node - AST node to check
* @returns true if node is ts.BooleanLiteral
*/
function isBooleanLiteral(node: ts.Node): node is ts.BooleanLiteral;
/**
* Check if node is a null literal
* @param node - AST node to check
* @returns true if node is ts.NullLiteral
*/
function isNullLiteral(node: ts.Node): node is ts.NullLiteral;
/**
* Check if node is a BigInt literal (TypeScript 3.2+)
* @param node - AST node to check
* @returns true if node is ts.BigIntLiteral
*/
function isBigIntLiteral(node: ts.Node): node is ts.BigIntLiteral;
/**
* Check if node is a regular expression literal
* @param node - AST node to check
* @returns true if node is ts.RegularExpressionLiteral
*/
function isRegularExpressionLiteral(node: ts.Node): node is ts.RegularExpressionLiteral;
/**
* Check if node is a template expression with substitutions
* @param node - AST node to check
* @returns true if node is ts.TemplateExpression
*/
function isTemplateExpression(node: ts.Node): node is ts.TemplateExpression;
/**
* Check if node is a template literal without substitutions
* @param node - AST node to check
* @returns true if node is ts.NoSubstitutionTemplateLiteral
*/
function isNoSubstitutionTemplateLiteral(node: ts.Node): node is ts.NoSubstitutionTemplateLiteral;Type guards for all declaration constructs in TypeScript.
/**
* Check if node is a variable declaration
* @param node - AST node to check
* @returns true if node is ts.VariableDeclaration
*/
function isVariableDeclaration(node: ts.Node): node is ts.VariableDeclaration;
/**
* Check if node is a variable statement
* @param node - AST node to check
* @returns true if node is ts.VariableStatement
*/
function isVariableStatement(node: ts.Node): node is ts.VariableStatement;
/**
* Check if node is a function declaration
* @param node - AST node to check
* @returns true if node is ts.FunctionDeclaration
*/
function isFunctionDeclaration(node: ts.Node): node is ts.FunctionDeclaration;
/**
* Check if node is a class declaration
* @param node - AST node to check
* @returns true if node is ts.ClassDeclaration
*/
function isClassDeclaration(node: ts.Node): node is ts.ClassDeclaration;
/**
* Check if node is an interface declaration
* @param node - AST node to check
* @returns true if node is ts.InterfaceDeclaration
*/
function isInterfaceDeclaration(node: ts.Node): node is ts.InterfaceDeclaration;
/**
* Check if node is a type alias declaration
* @param node - AST node to check
* @returns true if node is ts.TypeAliasDeclaration
*/
function isTypeAliasDeclaration(node: ts.Node): node is ts.TypeAliasDeclaration;
/**
* Check if node is an enum declaration
* @param node - AST node to check
* @returns true if node is ts.EnumDeclaration
*/
function isEnumDeclaration(node: ts.Node): node is ts.EnumDeclaration;
/**
* Check if node is a module declaration
* @param node - AST node to check
* @returns true if node is ts.ModuleDeclaration
*/
function isModuleDeclaration(node: ts.Node): node is ts.ModuleDeclaration;
/**
* Check if node is an enum member
* @param node - AST node to check
* @returns true if node is ts.EnumMember
*/
function isEnumMember(node: ts.Node): node is ts.EnumMember;Type guards for function-like declarations and expressions.
/**
* Check if node is a function expression
* @param node - AST node to check
* @returns true if node is ts.FunctionExpression
*/
function isFunctionExpression(node: ts.Node): node is ts.FunctionExpression;
/**
* Check if node is an arrow function
* @param node - AST node to check
* @returns true if node is ts.ArrowFunction
*/
function isArrowFunction(node: ts.Node): node is ts.ArrowFunction;
/**
* Check if node is a method declaration
* @param node - AST node to check
* @returns true if node is ts.MethodDeclaration
*/
function isMethodDeclaration(node: ts.Node): node is ts.MethodDeclaration;
/**
* Check if node is a constructor declaration
* @param node - AST node to check
* @returns true if node is ts.ConstructorDeclaration
*/
function isConstructorDeclaration(node: ts.Node): node is ts.ConstructorDeclaration;
/**
* Check if node is a getter accessor declaration
* @param node - AST node to check
* @returns true if node is ts.GetAccessorDeclaration
*/
function isGetAccessorDeclaration(node: ts.Node): node is ts.GetAccessorDeclaration;
/**
* Check if node is a setter accessor declaration
* @param node - AST node to check
* @returns true if node is ts.SetAccessorDeclaration
*/
function isSetAccessorDeclaration(node: ts.Node): node is ts.SetAccessorDeclaration;Type guards for property access patterns and object property assignments.
/**
* Check if node is a property access expression (obj.prop)
* @param node - AST node to check
* @returns true if node is ts.PropertyAccessExpression
*/
function isPropertyAccessExpression(node: ts.Node): node is ts.PropertyAccessExpression;
/**
* Check if node is an element access expression (obj[key])
* @param node - AST node to check
* @returns true if node is ts.ElementAccessExpression
*/
function isElementAccessExpression(node: ts.Node): node is ts.ElementAccessExpression;
/**
* Check if node is a property declaration
* @param node - AST node to check
* @returns true if node is ts.PropertyDeclaration
*/
function isPropertyDeclaration(node: ts.Node): node is ts.PropertyDeclaration;
/**
* Check if node is a property assignment in object literal
* @param node - AST node to check
* @returns true if node is ts.PropertyAssignment
*/
function isPropertyAssignment(node: ts.Node): node is ts.PropertyAssignment;
/**
* Check if node is a shorthand property assignment ({prop})
* @param node - AST node to check
* @returns true if node is ts.ShorthandPropertyAssignment
*/
function isShorthandPropertyAssignment(node: ts.Node): node is ts.ShorthandPropertyAssignment;
/**
* Check if node is a spread assignment in object literal ({...obj})
* @param node - AST node to check
* @returns true if node is ts.SpreadAssignment
*/
function isSpreadAssignment(node: ts.Node): node is ts.SpreadAssignment;Type guards for function calls and object instantiation.
/**
* Check if node is a call expression
* @param node - AST node to check
* @returns true if node is ts.CallExpression
*/
function isCallExpression(node: ts.Node): node is ts.CallExpression;
/**
* Check if node is a new expression
* @param node - AST node to check
* @returns true if node is ts.NewExpression
*/
function isNewExpression(node: ts.Node): node is ts.NewExpression;
/**
* Check if node is a tagged template expression
* @param node - AST node to check
* @returns true if node is ts.TaggedTemplateExpression
*/
function isTaggedTemplateExpression(node: ts.Node): node is ts.TaggedTemplateExpression;Type guards for control flow constructs including conditionals, loops, and flow control.
/**
* Check if node is an if statement
* @param node - AST node to check
* @returns true if node is ts.IfStatement
*/
function isIfStatement(node: ts.Node): node is ts.IfStatement;
/**
* Check if node is a for statement
* @param node - AST node to check
* @returns true if node is ts.ForStatement
*/
function isForStatement(node: ts.Node): node is ts.ForStatement;
/**
* Check if node is a for-in statement
* @param node - AST node to check
* @returns true if node is ts.ForInStatement
*/
function isForInStatement(node: ts.Node): node is ts.ForInStatement;
/**
* Check if node is a for-of statement
* @param node - AST node to check
* @returns true if node is ts.ForOfStatement
*/
function isForOfStatement(node: ts.Node): node is ts.ForOfStatement;
/**
* Check if node is a while statement
* @param node - AST node to check
* @returns true if node is ts.WhileStatement
*/
function isWhileStatement(node: ts.Node): node is ts.WhileStatement;
/**
* Check if node is a do-while statement
* @param node - AST node to check
* @returns true if node is ts.DoStatement
*/
function isDoStatement(node: ts.Node): node is ts.DoStatement;
/**
* Check if node is a switch statement
* @param node - AST node to check
* @returns true if node is ts.SwitchStatement
*/
function isSwitchStatement(node: ts.Node): node is ts.SwitchStatement;
/**
* Check if node is a case clause
* @param node - AST node to check
* @returns true if node is ts.CaseClause
*/
function isCaseClause(node: ts.Node): node is ts.CaseClause;
/**
* Check if node is a default clause
* @param node - AST node to check
* @returns true if node is ts.DefaultClause
*/
function isDefaultClause(node: ts.Node): node is ts.DefaultClause;
/**
* Check if node is a break statement
* @param node - AST node to check
* @returns true if node is ts.BreakStatement
*/
function isBreakStatement(node: ts.Node): node is ts.BreakStatement;
/**
* Check if node is a continue statement
* @param node - AST node to check
* @returns true if node is ts.ContinueStatement
*/
function isContinueStatement(node: ts.Node): node is ts.ContinueStatement;
/**
* Check if node is a return statement
* @param node - AST node to check
* @returns true if node is ts.ReturnStatement
*/
function isReturnStatement(node: ts.Node): node is ts.ReturnStatement;
/**
* Check if node is a throw statement
* @param node - AST node to check
* @returns true if node is ts.ThrowStatement
*/
function isThrowStatement(node: ts.Node): node is ts.ThrowStatement;
/**
* Check if node is a try statement
* @param node - AST node to check
* @returns true if node is ts.TryStatement
*/
function isTryStatement(node: ts.Node): node is ts.TryStatement;Type guards for TypeScript type annotation nodes.
/**
* Check if node is a type reference node
* @param node - AST node to check
* @returns true if node is ts.TypeReferenceNode
*/
function isTypeReferenceNode(node: ts.Node): node is ts.TypeReferenceNode;
/**
* Check if node is a type literal node
* @param node - AST node to check
* @returns true if node is ts.TypeLiteralNode
*/
function isTypeLiteralNode(node: ts.Node): node is ts.TypeLiteralNode;
/**
* Check if node is a union type node
* @param node - AST node to check
* @returns true if node is ts.UnionTypeNode
*/
function isUnionTypeNode(node: ts.Node): node is ts.UnionTypeNode;
/**
* Check if node is an intersection type node
* @param node - AST node to check
* @returns true if node is ts.IntersectionTypeNode
*/
function isIntersectionTypeNode(node: ts.Node): node is ts.IntersectionTypeNode;
/**
* Check if node is a tuple type node
* @param node - AST node to check
* @returns true if node is ts.TupleTypeNode
*/
function isTupleTypeNode(node: ts.Node): node is ts.TupleTypeNode;
/**
* Check if node is an array type node
* @param node - AST node to check
* @returns true if node is ts.ArrayTypeNode
*/
function isArrayTypeNode(node: ts.Node): node is ts.ArrayTypeNode;
/**
* Check if node is a function type node
* @param node - AST node to check
* @returns true if node is ts.FunctionTypeNode
*/
function isFunctionTypeNode(node: ts.Node): node is ts.FunctionTypeNode;
/**
* Check if node is a constructor type node
* @param node - AST node to check
* @returns true if node is ts.ConstructorTypeNode
*/
function isConstructorTypeNode(node: ts.Node): node is ts.ConstructorTypeNode;
/**
* Check if node is a conditional type node
* @param node - AST node to check
* @returns true if node is ts.ConditionalTypeNode
*/
function isConditionalTypeNode(node: ts.Node): node is ts.ConditionalTypeNode;
/**
* Check if node is an infer type node
* @param node - AST node to check
* @returns true if node is ts.InferTypeNode
*/
function isInferTypeNode(node: ts.Node): node is ts.InferTypeNode;
/**
* Check if node is a mapped type node
* @param node - AST node to check
* @returns true if node is ts.MappedTypeNode
*/
function isMappedTypeNode(node: ts.Node): node is ts.MappedTypeNode;
/**
* Check if node is a literal type node
* @param node - AST node to check
* @returns true if node is ts.LiteralTypeNode
*/
function isLiteralTypeNode(node: ts.Node): node is ts.LiteralTypeNode;
/**
* Check if node is an indexed access type node
* @param node - AST node to check
* @returns true if node is ts.IndexedAccessTypeNode
*/
function isIndexedAccessTypeNode(node: ts.Node): node is ts.IndexedAccessTypeNode;
/**
* Check if node is a type operator node (keyof, typeof, etc.)
* @param node - AST node to check
* @returns true if node is ts.TypeOperatorNode
*/
function isTypeOperatorNode(node: ts.Node): node is ts.TypeOperatorNode;
/**
* Check if node is a type query node (typeof expression)
* @param node - AST node to check
* @returns true if node is ts.TypeQueryNode
*/
function isTypeQueryNode(node: ts.Node): node is ts.TypeQueryNode;
/**
* Check if node is an optional type node (TypeScript 3.0+)
* @param node - AST node to check
* @returns true if node is ts.OptionalTypeNode
*/
function isOptionalTypeNode(node: ts.Node): node is ts.OptionalTypeNode;
/**
* Check if node is a rest type node (TypeScript 3.0+)
* @param node - AST node to check
* @returns true if node is ts.RestTypeNode
*/
function isRestTypeNode(node: ts.Node): node is ts.RestTypeNode;Type guards for module import and export constructs.
/**
* Check if node is an import declaration
* @param node - AST node to check
* @returns true if node is ts.ImportDeclaration
*/
function isImportDeclaration(node: ts.Node): node is ts.ImportDeclaration;
/**
* Check if node is an import clause
* @param node - AST node to check
* @returns true if node is ts.ImportClause
*/
function isImportClause(node: ts.Node): node is ts.ImportClause;
/**
* Check if node is a namespace import (* as name)
* @param node - AST node to check
* @returns true if node is ts.NamespaceImport
*/
function isNamespaceImport(node: ts.Node): node is ts.NamespaceImport;
/**
* Check if node is named imports ({name1, name2})
* @param node - AST node to check
* @returns true if node is ts.NamedImports
*/
function isNamedImports(node: ts.Node): node is ts.NamedImports;
/**
* Check if node is an import specifier
* @param node - AST node to check
* @returns true if node is ts.ImportSpecifier
*/
function isImportSpecifier(node: ts.Node): node is ts.ImportSpecifier;
/**
* Check if node is an import equals declaration
* @param node - AST node to check
* @returns true if node is ts.ImportEqualsDeclaration
*/
function isImportEqualsDeclaration(node: ts.Node): node is ts.ImportEqualsDeclaration;
/**
* Check if node is an export declaration
* @param node - AST node to check
* @returns true if node is ts.ExportDeclaration
*/
function isExportDeclaration(node: ts.Node): node is ts.ExportDeclaration;
/**
* Check if node is an export assignment
* @param node - AST node to check
* @returns true if node is ts.ExportAssignment
*/
function isExportAssignment(node: ts.Node): node is ts.ExportAssignment;
/**
* Check if node is an export specifier
* @param node - AST node to check
* @returns true if node is ts.ExportSpecifier
*/
function isExportSpecifier(node: ts.Node): node is ts.ExportSpecifier;
/**
* Check if node is named exports ({name1, name2})
* @param node - AST node to check
* @returns true if node is ts.NamedExports
*/
function isNamedExports(node: ts.Node): node is ts.NamedExports;
/**
* Check if node is an import type node (TypeScript 2.9+)
* @param node - AST node to check
* @returns true if node is ts.ImportTypeNode
*/
function isImportTypeNode(node: ts.Node): node is ts.ImportTypeNode;Type guards for JSX syntax elements.
/**
* Check if node is a JSX element
* @param node - AST node to check
* @returns true if node is ts.JsxElement
*/
function isJsxElement(node: ts.Node): node is ts.JsxElement;
/**
* Check if node is a JSX self-closing element
* @param node - AST node to check
* @returns true if node is ts.JsxSelfClosingElement
*/
function isJsxSelfClosingElement(node: ts.Node): node is ts.JsxSelfClosingElement;
/**
* Check if node is a JSX opening element
* @param node - AST node to check
* @returns true if node is ts.JsxOpeningElement
*/
function isJsxOpeningElement(node: ts.Node): node is ts.JsxOpeningElement;
/**
* Check if node is a JSX closing element
* @param node - AST node to check
* @returns true if node is ts.JsxClosingElement
*/
function isJsxClosingElement(node: ts.Node): node is ts.JsxClosingElement;
/**
* Check if node is a JSX fragment
* @param node - AST node to check
* @returns true if node is ts.JsxFragment
*/
function isJsxFragment(node: ts.Node): node is ts.JsxFragment;
/**
* Check if node is a JSX opening fragment
* @param node - AST node to check
* @returns true if node is ts.JsxOpeningFragment
*/
function isJsxOpeningFragment(node: ts.Node): node is ts.JsxOpeningFragment;
/**
* Check if node is a JSX closing fragment
* @param node - AST node to check
* @returns true if node is ts.JsxClosingFragment
*/
function isJsxClosingFragment(node: ts.Node): node is ts.JsxClosingFragment;
/**
* Check if node is a JSX attribute
* @param node - AST node to check
* @returns true if node is ts.JsxAttribute
*/
function isJsxAttribute(node: ts.Node): node is ts.JsxAttribute;
/**
* Check if node is JSX attributes
* @param node - AST node to check
* @returns true if node is ts.JsxAttributes
*/
function isJsxAttributes(node: ts.Node): node is ts.JsxAttributes;
/**
* Check if node is a JSX spread attribute
* @param node - AST node to check
* @returns true if node is ts.JsxSpreadAttribute
*/
function isJsxSpreadAttribute(node: ts.Node): node is ts.JsxSpreadAttribute;
/**
* Check if node is a JSX expression
* @param node - AST node to check
* @returns true if node is ts.JsxExpression
*/
function isJsxExpression(node: ts.Node): node is ts.JsxExpression;
/**
* Check if node is JSX text
* @param node - AST node to check
* @returns true if node is ts.JsxText
*/
function isJsxText(node: ts.Node): node is ts.JsxText;Composite type guards that check for multiple related node types.
/**
* Check if node is block-like (can contain statements)
* @param node - AST node to check
* @returns true if node is ts.BlockLike
*/
function isBlockLike(node: ts.Node): node is ts.BlockLike;
/**
* Check if node is an entity name (identifier or qualified name)
* @param node - AST node to check
* @returns true if node is ts.EntityName
*/
function isEntityName(node: ts.Node): node is ts.EntityName;
/**
* Check if node is an entity name expression
* @param node - AST node to check
* @returns true if node is ts.EntityNameExpression
*/
function isEntityNameExpression(node: ts.Node): node is ts.EntityNameExpression;
/**
* Check if node is a type assertion expression
* @param node - AST node to check
* @returns true if node is ts.AssertionExpression
*/
function isAssertionExpression(node: ts.Node): node is ts.AssertionExpression;
/**
* Check if node is a call-like expression
* @param node - AST node to check
* @returns true if node is ts.CallLikeExpression
*/
function isCallLikeExpression(node: ts.Node): node is ts.CallLikeExpression;
/**
* Check if node is an iteration statement (for/while/do)
* @param node - AST node to check
* @returns true if node is ts.IterationStatement
*/
function isIterationStatement(node: ts.Node): node is ts.IterationStatement;
/**
* Check if node is numeric or string-like literal
* @param node - AST node to check
* @returns true if node is numeric, string, or template literal
*/
function isNumericOrStringLikeLiteral(node: ts.Node): node is ts.NumericLiteral | ts.StringLiteral | ts.NoSubstitutionTemplateLiteral;
/**
* Check if node is textual literal (string or template)
* @param node - AST node to check
* @returns true if node is ts.StringLiteral or ts.NoSubstitutionTemplateLiteral
*/
function isTextualLiteral(node: ts.Node): node is ts.StringLiteral | ts.NoSubstitutionTemplateLiteral;Usage Example:
import * as ts from "typescript";
import { isIdentifier, isCallExpression, isStringLiteral } from "tsutils/typeguard";
function analyzeNode(node: ts.Node) {
if (isIdentifier(node)) {
// node is now typed as ts.Identifier
console.log("Identifier name:", node.text);
console.log("Original keyword kind:", node.originalKeywordKind);
}
if (isCallExpression(node)) {
// node is now typed as ts.CallExpression
console.log("Function expression:", node.expression);
console.log("Argument count:", node.arguments.length);
}
if (isStringLiteral(node)) {
// node is now typed as ts.StringLiteral
console.log("String value:", node.text);
}
}Install with Tessl CLI
npx tessl i tessl/npm-tsutils