Skip to content

类型层接口

TypeChecker 返回的所有类型对象都实现 Type 接口。类型对象是不可变的,由 TypeChecker 统一管理和缓存。


Type — 基础接口

ts
interface Type {
  flags: TypeFlags;
  symbol: Symbol;
  aliasSymbol?: Symbol;
  aliasTypeArguments?: readonly Type[];
}

属性

成员类型说明
flagsTypeFlags最重要字段,类型分类位掩码
symbolSymbol关联的符号(非原始类型有此字段)
aliasSymbolSymbol?若该类型来自 type 别名,则为别名符号
aliasTypeArgumentsreadonly Type[]?别名的类型实参

方法

方法签名说明
getFlags(): TypeFlags返回 flags
getSymbol(): Symbol | undefinedsymbol 字段
getProperties(): Symbol[]该类型的可枚举属性符号列表
getProperty(name): Symbol | undefined按名称获取属性符号
getApparentProperties(): Symbol[]含继承属性的完整列表
getCallSignatures(): readonly Signature[]调用签名列表
getConstructSignatures(): readonly Signature[]构造签名列表
getStringIndexType(): Type | undefined字符串索引类型
getNumberIndexType(): Type | undefined数字索引类型
getBaseTypes(): BaseType[] | undefined基类型(class / interface)
getNonNullableType(): Type去掉 null / undefined
getConstraint(): Type | undefined泛型约束
getDefault(): Type | undefined泛型默认值
isUnion(): this is UnionType是联合类型
isIntersection(): this is IntersectionType是交叉类型
isUnionOrIntersection(): this is UnionOrIntersectionType是联合或交叉
isLiteral(): this is LiteralType是字面量类型
isStringLiteral(): this is StringLiteralType是字符串字面量
isNumberLiteral(): this is NumberLiteralType是数字字面量
isTypeParameter(): this is TypeParameter是类型参数 T
isClassOrInterface(): this is InterfaceType是 class 或 interface
isClass(): this is InterfaceType是 class
isIndexType(): this is IndexTypekeyof T 类型

TypeFlags

TypeFlags 是位掩码枚举,单个类型对象可同时拥有多个标志。

ts
// 典型用法
if (type.flags & ts.TypeFlags.StringLiteral) {
  const lit = type as ts.StringLiteralType;
  console.log(lit.value);
}

原始类型标志

标志说明
Any1any
Unknown2unknown
String4string
Number8number
Boolean16boolean
Enum32枚举类型
BigInt64bigint
ESSymbol4096symbol(ES 内建)
UniqueESSymbol8192unique symbol
Void16384void
Undefined32768undefined
Null65536null
Never131072never
NonPrimitive67108864object(小写)

字面量类型标志

标志说明
StringLiteral字符串字面量 "foo"
NumberLiteral数字字面量 42
BooleanLiteraltrue / false
EnumLiteral枚举成员字面量
BigIntLiteralbigint 字面量

复合 / 结构类型标志

标志说明
TypeParameter类型参数 T
Object对象类型(进一步用 ObjectFlags 区分)
Union联合类型 A | B
Intersection交叉类型 A & B
Indexkeyof T
IndexedAccessT[K]
Conditional条件类型 T extends U ? X : Y
Substitution替换类型(TypeChecker 内部)
TemplateLiteral模板字面量类型
StringMappingUppercase<T> / Lowercase<T> 等内建字符串映射

复合别名标志

标志等价说明
AnyOrUnknownAny | Unknown
NullableUndefined | Null可空
LiteralStringLiteral | NumberLiteral | BigIntLiteral | BooleanLiteral所有字面量
UnitLiteral | UniqueESSymbol | Nullable单元类型
PrimitiveString | Number | BigInt | Boolean | Enum | ESSymbol | Void | Undefined | Null所有原始类型
StringOrNumberLiteralStringLiteral | NumberLiteral

字面量类型

LiteralType

ts
interface LiteralType extends Type {
  value: string | number | PseudoBigInt;
  freshType: LiteralType;    // fresh 版本(刚被推断,溢出检查用)
  regularType: LiteralType;  // regular 版本(widened 后)
}

interface StringLiteralType extends LiteralType { value: string; }
interface NumberLiteralType extends LiteralType { value: number; }
interface BigIntLiteralType extends LiteralType { value: PseudoBigInt; }

UniqueESSymbolType

ts
interface UniqueESSymbolType extends Type {
  symbol: Symbol;
  escapedName: __String;
}

联合 / 交叉类型

ts
interface UnionOrIntersectionType extends Type {
  types: readonly Type[];  // 成员类型列表
}

interface UnionType extends UnionOrIntersectionType {}
interface IntersectionType extends UnionOrIntersectionType {}

TypeParameter

ts
interface TypeParameter extends InstantiableType {
  constraint: Type | undefined;       // T extends U 中的 U
  default: Type | undefined;          // T = Default
  target?: TypeParameter;             // 泛型实例化的原始参数
  mapper?: TypeMapper;                // 实例化时的类型映射器
  isThisType?: boolean;               // 是否为 this 类型
  resolvedDefaultType?: Type;
}

ObjectType 与 ObjectFlags

type.flags & TypeFlags.Object 为真,该类型即为 ObjectType,通过 objectFlags 进一步区分。

ts
interface ObjectType extends Type {
  objectFlags: ObjectFlags;
}

ObjectFlags

标志说明
Class1class 声明
Interface2interface 声明
Reference4泛型实例化引用,如 Array<string>
Tuple8元组类型
Anonymous16匿名对象类型
Mapped32映射类型
Instantiated64已实例化的映射类型
ObjectLiteral128对象字面量类型
EvolvingArray256推演数组(内部)
ObjectLiteralPatternWithComputedProperties512含计算属性的对象字面量模式
ReverseMapped1024反向映射类型
JsxAttributes2048JSX 属性类型
JSLiteral4096JS 字面量类型
FreshLiteral8192Fresh 字面量对象(溢出检查用)
ArrayLiteral16384数组字面量类型
ContainsSpread2097152含散展的对象类型
ObjectRestType4194304对象 rest 类型
InstantiationExpressionType8388608实例化表达式类型(F<T>
SingleSignatureType134217728单一签名类型
ClassOrInterface3Class | Interface(复合)

InstantiableType

InstantiableType 是所有“可实例化”类型的基类,包括 TypeParameterConditionalTypeIndexedAccessTypeIndexTypeTemplateLiteralTypeStringMappingType

ts
interface InstantiableType extends Type {}

StringMappingType

StringMappingType 对应 Uppercase<T> / Lowercase<T> / Capitalize<T> / Uncapitalize<T> 等内建字符串映射类型(TypeFlags.StringMapping)。

ts
interface StringMappingType extends InstantiableType {
  symbol: Symbol;  // 对应的内建符号(Uppercase/Lowercase 等)
  type: Type;      // 被映射的类型
}

SubstitutionType

SubstitutionType 是 TypeChecker 内部使用的替换类型(TypeFlags.Substitution),在条件类型的真分支推断中用于将类型局限为更精确的类型。

ts
interface SubstitutionType extends InstantiableType {
  objectFlags: ObjectFlags;
  baseType: Type;    // 局限前的基类型(T)
  constraint: Type;  // 局限后的类型(负载更精确的类型)
}

InterfaceType / InterfaceTypeWithDeclaredMembers

ts
interface InterfaceType extends ObjectType {
  typeParameters: TypeParameter[] | undefined;
  outerTypeParameters: TypeParameter[] | undefined;
  localTypeParameters: TypeParameter[] | undefined;
  thisType: TypeParameter | undefined;
  resolvedBaseConstructorType?: Type;
  resolvedBaseTypes: BaseType[];
}

interface InterfaceTypeWithDeclaredMembers extends InterfaceType {
  declaredProperties: Symbol[];
  declaredCallSignatures: Signature[];
  declaredConstructSignatures: Signature[];
  declaredIndexInfos: IndexInfo[];
}

TypeReference(泛型实例)

ts
interface TypeReference extends ObjectType {
  target: GenericType;                          // 原始泛型类型,如 Array<T>
  node?: TypeReferenceNode | ArrayTypeNode | TupleTypeNode;
  mapper?: TypeMapper;
  resolvedTypeArguments?: readonly Type[];       // 已解析的类型实参
  literalType?: TypeReference;
}

TIP

通过 checker.getTypeArguments(typeRef) 安全地获取类型实参,而非直接读 resolvedTypeArguments


TupleType / TupleTypeReference

ts
interface TupleType extends GenericType {
  elementFlags: readonly ElementFlags[];
  minLength: number;        // 最少需要的元素个数
  fixedLength: number;      // 固定长度(可选前)
  hasRestElement: boolean;
  combinedFlags: ElementFlags;
  readonly: boolean;
  labeledElementDeclarations?: readonly (NamedTupleMember | ParameterDeclaration)[];
}

const enum ElementFlags {
  Required  = 1,   // 必须元素
  Optional  = 2,   // 可选元素 T?
  Rest      = 4,   // Rest 元素 ...T[]
  Variadic  = 8,   // Variadic 元素 ...T
  Variable  = Rest | Variadic,
}

ConditionalType

ts
interface ConditionalType extends InstantiableType {
  root: ConditionalRoot;
  checkType: Type;             // T extends U 中的 T
  extendsType: Type;           // T extends U 中的 U
  resolvedTrueType?: Type;
  resolvedFalseType?: Type;
  resolvedInferredTrueType?: Type;
  mapper?: TypeMapper;
  combinedMapper?: TypeMapper;
}

MappedType

ts
interface MappedType extends AnonymousType {
  declaration: MappedTypeNode;
  typeParameter?: TypeParameter;   // 映射变量 K
  constraintType?: Type;           // 约束类型(keyof T)
  nameType?: Type;                 // as 子句产生的名称类型
  templateType?: Type;             // 映射值类型
  modifiersType?: Type;
  resolvedApparentType?: Type;
}

IndexType(keyof T)

ts
interface IndexType extends InstantiableType {
  type: InstantiableType | UnionOrIntersectionType;
}

IndexedAccessType(T[K])

ts
interface IndexedAccessType extends InstantiableType {
  objectType: Type;   // T
  indexType: Type;    // K
  constraint?: Type;
  simplifiedForReading?: Type;
  simplifiedForWriting?: Type;
}

TemplateLiteralType

ts
interface TemplateLiteralType extends InstantiableType {
  texts: readonly string[];  // 固定片段,长度 = types.length + 1
  types: readonly Type[];    // 插值类型列表
}

Signature(函数 / 调用签名)

ts
interface Signature {
  declaration?: SignatureDeclaration | JSDocSignature;
  typeParameters?: readonly TypeParameter[];
  parameters: readonly Symbol[];
  thisParameter?: Symbol;
  resolvedReturnType?: Type;
  resolvedTypePredicate?: TypePredicate;
  minArgumentCount: number;
  flags: SignatureFlags;
}

方法

方法签名说明
getDeclaration(): SignatureDeclaration获取声明节点
getTypeParameters(): TypeParameter[] | undefined泛型参数
getParameters(): Symbol[]参数符号列表
getTypeParameterAtPosition(pos): Type按位置获取泛型参数类型
getReturnType(): Type返回类型
getDocumentationComment(checker?): SymbolDisplayPart[]JSDoc 说明
getJsDocTags(checker?): JSDocTagInfo[]JSDoc 标签

SignatureFlags

标志说明
None
HasRestParameter有 rest 参数
HasLiteralTypes含字面量类型
Abstract抽象
IsInnerCallChain内部调用链(可选链内)
IsOuterCallChain外部调用链
IsUntypedSignatureInJSFileJS 文件无类型签名
IsNonInferrable不可推断
PropagatingFlagsHasRestParameter | HasLiteralTypes | IsUntypedSignatureInJSFile

IndexInfo(索引签名)

ts
interface IndexInfo {
  keyType: Type;     // string / number / symbol
  type: Type;        // 值类型
  isReadonly: boolean;
  declaration?: IndexSignatureDeclaration;
}

TypePredicate(类型谓词)

ts
interface TypePredicate {
  kind: TypePredicateKind;
  parameterName: string | undefined;   // identifier 或 "this"
  parameterIndex: number | undefined;
  type: Type | undefined;              // 断言为的类型
}

const enum TypePredicateKind {
  This              = 0,  // this is T
  Identifier        = 1,  // x is T
  AssertsThis       = 2,  // asserts this is T
  AssertsIdentifier = 3,  // asserts x is T
}