类型收窄
TypeScript 使用收窄(narrowing)在条件分支中细化值的类型。Auto 使用 is 表达式进行模式匹配,提供了更结构化的方式来处理同一问题。
使用 is 进行模式匹配
Auto 的 is 表达式检查值并与模式匹配。当模式匹配时,执行对应的分支。 这是 Auto 收窄类型的主要机制——它用统一的语法替代了 TypeScript 的 typeof 检查、instanceof 检查和可辨识联合 switch。
is 表达式转译为 TypeScript 的 switch 语句。每个模式绑定变量(如 n 和 t),这些变量在分支体内可用。在 TypeScript 中,等价的做法是 对可辨识联合的 _tag 字段使用 switch。
真值收窄
Auto 将 0、空字符串、null 和 false 视为假值——与 TypeScript 相同。 你可以直接在 if 条件中使用任何值:
这在 Auto 和 TypeScript 中行为完全相同。if count 条件隐式收窄类型 ——在 if 分支内,编译器知道 count 为真值。
可辨识联合
TypeScript 的可辨识联合使用公共属性(如 kind)来收窄类型。Auto 使用 带标签变体的枚举,自动转译为可辨识联合:
每个枚举变体变成一个 { _tag: "...", value: T } 对象。is 表达式 转译为对 _tag 字段的 switch,这正是 TypeScript 处理可辨识联合的方式。
穷尽性检查
Auto 的 is 表达式要求处理每个变体。如果你向枚举添加新变体但忘记 在 is 表达式中处理它,编译器会发出警告。TypeScript 通过 switch 默认分支中的 never 类型检查实现相同的效果。
TypeScript 独有的收窄功能
以下 TypeScript 收窄功能在 Auto 中没有直接对应物,因为 Auto 使用 is 模式匹配作为统一的收窄机制。
typeof 守卫
// 仅限 TypeScript
function format(value: string | number) {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}在 Auto 中,请改用 is 模式匹配与枚举变体。
instanceof 收窄
// 仅限 TypeScript
function formatDate(date: Date | string) {
if (date instanceof Date) {
return date.toISOString();
}
return date;
}Auto 没有带运行时类型信息的类。请改用基于枚举的标签。
类型谓词
// 仅限 TypeScript
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined;
}Auto 的 is 表达式隐式处理了这一点——每个分支已经具有收窄后的类型。
快速参考
| TypeScript | Auto | 描述 |
|---|---|---|
typeof x === "string" | x is str => ... | 类型检查 |
x instanceof Date | 枚举标签 | 运行时类型检查 |
switch (x.kind) | x is { ... } | 可辨识联合 |
if (value) | if value | 真值检查 |
x is Type(谓词) | is 分支绑定 | 类型谓词 |