Comprehensive TypeScript type definitions for the SWC JavaScript/TypeScript transformation and minification APIs.
—
TypeScript-specific AST nodes and type system representations for TypeScript compilation and analysis. These types cover the complete TypeScript syntax including type annotations, generics, interfaces, and advanced type constructs.
Core TypeScript type representations covering all type expressions.
/**
* All TypeScript type expressions
*/
type TsType =
| TsKeywordType | TsThisType | TsFnOrConstructorType
| TsTypeReference | TsTypeQuery | TsTypeLiteral
| TsArrayType | TsTupleType | TsOptionalType
| TsRestType | TsUnionOrIntersectionType
| TsConditionalType | TsInferType | TsParenthesizedType
| TsTypeOperator | TsIndexedAccessType | TsMappedType
| TsLiteralType | TsTypePredicate | TsImportType;
/**
* Primitive TypeScript keyword types
*/
interface TsKeywordType extends Node, HasSpan {
type: "TsKeywordType";
kind: TsKeywordTypeKind;
}
type TsKeywordTypeKind =
| "any" | "unknown" | "number" | "object" | "boolean"
| "bigint" | "string" | "symbol" | "void" | "undefined"
| "null" | "never" | "intrinsic";
/**
* 'this' type reference
*/
interface TsThisType extends Node, HasSpan {
type: "TsThisType";
}
/**
* Type reference (e.g., Array<T>, MyInterface)
*/
interface TsTypeReference extends Node, HasSpan {
type: "TsTypeReference";
typeName: TsEntityName;
typeParams?: TsTypeParameterInstantiation;
}
/**
* Union and intersection types
*/
type TsUnionOrIntersectionType = TsUnionType | TsIntersectionType;
interface TsUnionType extends Node, HasSpan {
type: "TsUnionType";
types: TsType[];
}
interface TsIntersectionType extends Node, HasSpan {
type: "TsIntersectionType";
types: TsType[];
}Type definitions for callable types.
type TsFnOrConstructorType = TsFunctionType | TsConstructorType;
/**
* Function type signature
*/
interface TsFunctionType extends Node, HasSpan {
type: "TsFunctionType";
params: TsFnParameter[];
typeParams?: TsTypeParameterDeclaration;
typeAnnotation: TsTypeAnnotation;
}
/**
* Constructor type signature
*/
interface TsConstructorType extends Node, HasSpan {
type: "TsConstructorType";
params: TsFnParameter[];
typeParams?: TsTypeParameterDeclaration;
typeAnnotation: TsTypeAnnotation;
isAbstract: boolean;
}
type TsFnParameter =
| BindingIdentifier | ArrayPattern | RestElement | ObjectPattern;Advanced TypeScript type system features.
/**
* Array type (T[])
*/
interface TsArrayType extends Node, HasSpan {
type: "TsArrayType";
elemType: TsType;
}
/**
* Tuple type ([T, U, V])
*/
interface TsTupleType extends Node, HasSpan {
type: "TsTupleType";
elemTypes: TsTupleElement[];
}
interface TsTupleElement extends Node, HasSpan {
type: "TsTupleElement";
label?: Pattern;
ty: TsType;
}
/**
* Conditional type (T extends U ? X : Y)
*/
interface TsConditionalType extends Node, HasSpan {
type: "TsConditionalType";
checkType: TsType;
extendsType: TsType;
trueType: TsType;
falseType: TsType;
}
/**
* Mapped type ({ [K in T]: U })
*/
interface TsMappedType extends Node, HasSpan {
type: "TsMappedType";
readonly?: TruePlusMinus;
typeParam: TsTypeParameter;
nameType?: TsType;
optional?: TruePlusMinus;
typeAnnotation?: TsType;
}
type TruePlusMinus = true | "+" | "-";
/**
* Indexed access type (T[K])
*/
interface TsIndexedAccessType extends Node, HasSpan {
type: "TsIndexedAccessType";
readonly: boolean;
objectType: TsType;
indexType: TsType;
}
/**
* Type operator (keyof T, readonly T, unique T)
*/
interface TsTypeOperator extends Node, HasSpan {
type: "TsTypeOperator";
op: TsTypeOperatorOp;
typeAnnotation: TsType;
}
type TsTypeOperatorOp = "keyof" | "unique" | "readonly";TypeScript declaration types including interfaces, type aliases, and enums.
/**
* Interface declaration
*/
interface TsInterfaceDeclaration extends Node, HasSpan {
type: "TsInterfaceDeclaration";
id: Identifier;
declare: boolean;
typeParams?: TsTypeParameterDeclaration;
extends: TsExpressionWithTypeArguments[];
body: TsInterfaceBody;
}
interface TsInterfaceBody extends Node, HasSpan {
type: "TsInterfaceBody";
body: TsTypeElement[];
}
/**
* Type alias declaration
*/
interface TsTypeAliasDeclaration extends Node, HasSpan {
type: "TsTypeAliasDeclaration";
declare: boolean;
id: Identifier;
typeParams?: TsTypeParameterDeclaration;
typeAnnotation: TsType;
}
/**
* Enum declaration
*/
interface TsEnumDeclaration extends Node, HasSpan {
type: "TsEnumDeclaration";
declare: boolean;
isConst: boolean;
id: Identifier;
members: TsEnumMember[];
}
interface TsEnumMember extends Node, HasSpan {
type: "TsEnumMember";
id: TsEnumMemberId;
init?: Expression;
}
type TsEnumMemberId = Identifier | StringLiteral;Type elements used in interfaces and type literals.
type TsTypeElement =
| TsCallSignatureDeclaration | TsConstructSignatureDeclaration
| TsPropertySignature | TsGetterSignature | TsSetterSignature
| TsMethodSignature | TsIndexSignature;
/**
* Call signature in interface
*/
interface TsCallSignatureDeclaration extends Node, HasSpan {
type: "TsCallSignatureDeclaration";
params: TsFnParameter[];
typeAnnotation?: TsTypeAnnotation;
typeParams?: TsTypeParameterDeclaration;
}
/**
* Constructor signature in interface
*/
interface TsConstructSignatureDeclaration extends Node, HasSpan {
type: "TsConstructSignatureDeclaration";
params: TsFnParameter[];
typeAnnotation?: TsTypeAnnotation;
typeParams?: TsTypeParameterDeclaration;
}
/**
* Property signature in interface
*/
interface TsPropertySignature extends Node, HasSpan {
type: "TsPropertySignature";
readonly: boolean;
key: Expression;
computed: boolean;
optional: boolean;
typeAnnotation?: TsTypeAnnotation;
}
/**
* Method signature in interface
*/
interface TsMethodSignature extends Node, HasSpan {
type: "TsMethodSignature";
readonly: boolean;
key: Expression;
computed: boolean;
optional: boolean;
params: TsFnParameter[];
typeAnnotation?: TsTypeAnnotation;
typeParams?: TsTypeParameterDeclaration;
}
/**
* Index signature in interface
*/
interface TsIndexSignature extends Node, HasSpan {
type: "TsIndexSignature";
params: TsFnParameter[];
typeAnnotation?: TsTypeAnnotation;
readonly: boolean;
static: boolean;
}Generic type parameter definitions and instantiations.
/**
* Type parameter declaration (<T, U>)
*/
interface TsTypeParameterDeclaration extends Node, HasSpan {
type: "TsTypeParameterDeclaration";
parameters: TsTypeParameter[];
}
/**
* Individual type parameter
*/
interface TsTypeParameter extends Node, HasSpan {
type: "TsTypeParameter";
name: Identifier;
in: boolean;
out: boolean;
constraint?: TsType;
default?: TsType;
}
/**
* Type argument instantiation (<string, number>)
*/
interface TsTypeParameterInstantiation extends Node, HasSpan {
type: "TsTypeParameterInstantiation";
params: TsType[];
}Type annotations and type assertion expressions.
/**
* Type annotation (: Type)
*/
interface TsTypeAnnotation extends Node, HasSpan {
type: "TsTypeAnnotation";
typeAnnotation: TsType;
}
/**
* Type assertion (expr as Type)
*/
interface TsAsExpression extends ExpressionBase {
type: "TsAsExpression";
expression: Expression;
typeAnnotation: TsType;
}
/**
* Satisfies expression (expr satisfies Type)
*/
interface TsSatisfiesExpression extends ExpressionBase {
type: "TsSatisfiesExpression";
expression: Expression;
typeAnnotation: TsType;
}
/**
* Type assertion (<Type>expr)
*/
interface TsTypeAssertion extends ExpressionBase {
type: "TsTypeAssertion";
expression: Expression;
typeAnnotation: TsType;
}
/**
* Const assertion (expr as const)
*/
interface TsConstAssertion extends ExpressionBase {
type: "TsConstAssertion";
expression: Expression;
}
/**
* Non-null assertion (expr!)
*/
interface TsNonNullExpression extends ExpressionBase {
type: "TsNonNullExpression";
expression: Expression;
}TypeScript module and namespace syntax.
/**
* Module/namespace declaration
*/
interface TsModuleDeclaration extends Node, HasSpan {
type: "TsModuleDeclaration";
declare: boolean;
global: boolean;
id: TsModuleName;
body?: TsNamespaceBody;
}
/**
* Namespace declaration body
*/
type TsNamespaceBody = TsModuleBlock | TsNamespaceDeclaration;
interface TsModuleBlock extends Node, HasSpan {
type: "TsModuleBlock";
body: ModuleItem[];
}
interface TsNamespaceDeclaration extends Node, HasSpan {
type: "TsNamespaceDeclaration";
declare: boolean;
global: boolean;
id: Identifier;
body: TsNamespaceBody;
}
type TsModuleName = Identifier | StringLiteral;
/**
* Import equals declaration
*/
interface TsImportEqualsDeclaration extends Node, HasSpan {
type: "TsImportEqualsDeclaration";
declare: boolean;
isExport: boolean;
isTypeOnly: boolean;
id: Identifier;
moduleRef: TsModuleReference;
}
type TsModuleReference = TsEntityName | TsExternalModuleReference;
interface TsExternalModuleReference extends Node, HasSpan {
type: "TsExternalModuleReference";
expression: StringLiteral;
}Supporting types and utility interfaces for TypeScript features.
type TsEntityName = TsQualifiedName | Identifier;
interface TsQualifiedName extends Node {
type: "TsQualifiedName";
left: TsEntityName;
right: Identifier;
}
/**
* Type literal ({ key: Type })
*/
interface TsTypeLiteral extends Node, HasSpan {
type: "TsTypeLiteral";
members: TsTypeElement[];
}
/**
* Literal type ("string" | 42 | true)
*/
interface TsLiteralType extends Node, HasSpan {
type: "TsLiteralType";
literal: TsLiteral;
}
type TsLiteral =
| NumericLiteral | StringLiteral | BooleanLiteral
| BigIntLiteral | TsTemplateLiteralType;
/**
* Template literal type (`hello ${T}`)
*/
interface TsTemplateLiteralType extends Node, HasSpan {
type: "TemplateLiteral";
types: TsType[];
quasis: TemplateElement[];
}
/**
* Type predicate (arg is Type)
*/
interface TsTypePredicate extends Node, HasSpan {
type: "TsTypePredicate";
asserts: boolean;
paramName: TsThisTypeOrIdent;
typeAnnotation?: TsTypeAnnotation;
}
type TsThisTypeOrIdent = TsThisType | Identifier;
/**
* Import type (import("module").Type)
*/
interface TsImportType extends Node, HasSpan {
type: "TsImportType";
argument: StringLiteral;
qualifier?: TsEntityName;
typeArguments?: TsTypeParameterInstantiation;
}
/**
* Parameter property (constructor(public prop: Type))
*/
interface TsParameterProperty extends Node, HasSpan, HasDecorator {
type: "TsParameterProperty";
accessibility?: Accessibility;
override: boolean;
readonly: boolean;
param: TsParameterPropertyParameter;
}
type TsParameterPropertyParameter = BindingIdentifier | AssignmentPattern;Usage Examples:
import type {
TsType, TsInterfaceDeclaration, TsTypeAliasDeclaration,
TsTypeAnnotation, TsAsExpression
} from "@swc/types";
// Type-safe TypeScript AST manipulation
function extractInterfaceProperties(
decl: TsInterfaceDeclaration
): Array<{ name: string; type: TsType }> {
return decl.body.body
.filter(member => member.type === "TsPropertySignature")
.map(member => ({
name: member.key.type === "Identifier" ? member.key.value : "",
type: member.typeAnnotation?.typeAnnotation
}))
.filter(prop => prop.type !== undefined);
}
// Create type annotation
function createTypeAnnotation(tsType: TsType): TsTypeAnnotation {
return {
type: "TsTypeAnnotation",
span: { start: 0, end: 0, ctxt: 0 },
typeAnnotation: tsType
};
}
// Check if expression has type assertion
function hasTypeAssertion(expr: Expression): expr is TsAsExpression {
return expr.type === "TsAsExpression";
}Install with Tessl CLI
npx tessl i tessl/npm-swc--types