Skip to content

类型 Alias

一句话解释

类型 alias 是 name for 类型, 创建使用 `类型` keyword. 它能 alias primitives, unions, intersections, tuples, and 对象类型.

用大白话说

类型 alias gives name 到类型. 你创建它 使用 type Name = .... Unlike interfaces, type aliases can represent unions, intersections, tuples, and primitive types — not just object shapes.

Type aliases are erased at compile time, just like interfaces. They cannot be merged (unlike interfaces), but they are more flexible in what they can represent.

Use type aliases for unions (类型 状态 = "active" | "inactive"), intersections (类型 & B), tuples (类型 Pair = [字符串, 数字]), and primitive aliases (类型 ID = 字符串). Use interfaces for object shapes that might need declaration merging or extension.

示例

typescript
type Status = "active" | "inactive" | "pending";
type ID = string | number;
type Pair = [string, number];

const status: Status = "active";
const id: ID = 123;

类型 aliases for 字符串 union, 字符串|数字 union, and tuple.

常见混淆

  • 容易混淆的对象: 接口

    关键区别: 类型 aliases 能 represent unions and tuples; 接口 cannot. 接口 support declaration 合并; 类型 aliases cannot.

相关术语

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表