TypeScript Academy · 课时

typeof 与真值收窄

使用 typeof 检查收窄原始类型

第 1 / 4 课13 个步骤

typeof 与真值收窄 是 CoddyKit 上的免费 TypeScript Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 TypeScript Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 TypeScript Academy 课程共包含 4 节课。

欢迎

TypeScript 会在条件代码块中收窄联合类型。`typeof` 检查和真值检查是最常见的类型收窄技术。

typeof 类型收窄

使用 `typeof` 检查原始类型。TypeScript 能理解 typeof 检查,并在代码块中收窄类型。
function process(val: string | number) {
  if (typeof val === 'string') {
    return val.toUpperCase(); // val: string
  }
  return val.toFixed(2); // val: number
}

typeof 的返回值

typeof 会返回 'string'、'number'、'boolean'、'bigint'、'symbol'、'undefined'、'object' 或 'function'。注意:typeof null === 'object',这是 JavaScript 的一个特殊行为。
typeof 'hello'    // 'string'
typeof 42         // 'number'
typeof true       // 'boolean'
typeof null       // 'object' (quirk!)
typeof undefined  // 'undefined'

真值类型收窄

JavaScript 值可以是真值或假值。在真值检查中,TypeScript 会将 `T | null | undefined` 收窄为 `T`。
function greet(name: string | null) {
  if (name) {
    console.log('Hello, ' + name); // name: string
  } else {
    console.log('Hello, stranger');
  }
}

假值

JavaScript 中的假值包括:false、0、''、null、undefined、0n 和 NaN。真值类型收窄会在检查通过时从类型中移除这些值。

使用布尔转换进行类型收窄

使用 `Boolean(val)` 或 `!!val` 也会将 null 和 undefined 排除在外。
const items: (string | null)[] = ['a', null, 'b', null];
const strings = items.filter((x): x is string => x !== null);

取反条件

TypeScript 也会在 else 分支以及提前返回之后进行类型收窄。
function getLength(val: string | null): number {
  if (!val) return 0; // early return narrows
  return val.length;  // val: string here
}

结合 typeof 与相等性检查

将严格相等(===)与 typeof 结合使用,可以实现精确的类型收窄。
function accept(x: string | number | boolean) {
  if (typeof x === 'string' || typeof x === 'number') {
    console.log(x); // x: string | number
  }
}

typeof 无法收窄为 null

要收窄为 null,请使用严格相等检查 `=== null`。真值检查无法区分 null 与其他假值。
function check(x: string | null | 0) {
  if (x === null) { /* x: null */ }
  else if (x === 0) { /* x: 0 */ }
  else { /* x: string */ }
}

循环中的类型收窄

TypeScript 也会在循环体内应用类型收窄,而不仅仅是在 if 语句中应用。

控制流分析

TypeScript 使用控制流分析跟踪代码中每个位置的类型。赋值也会更新类型。
let x: string | number = 'hello';
// x: string
x = 42;
// x: number

快速检查

在 `if (typeof val === 'string')` 内部,假设 `val` 的类型原本是 `string | number`,那么它的类型是什么?

回顾

typeof 检查和真值条件可以在 TypeScript 中收窄联合类型。TypeScript 使用控制流分析跟踪每个位置的类型。请使用严格相等检查 null/undefined。
免费开始

用 AI 导师学习 TypeScript — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
101
课程
352

常见问题解答

「typeof 与真值收窄」课时是免费的吗?

是的 — 「typeof 与真值收窄」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 TypeScript Academy 课程的其余内容,请升级到 CoddyKit PRO。 TypeScript Academy 课程共包含 4 节课。

「typeof 与真值收窄」这节课中我会学到什么?

使用 typeof 检查收窄原始类型 你通过在浏览器中直接运行的动手代码来练习 TypeScript Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 TypeScript Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 TypeScript Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「typeof 与真值收窄」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 TypeScript Academy 课中编写并运行代码吗?

能。每节 TypeScript Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. typeof 与真值收窄
  2. instanceof 与 in 收窄
  3. 用户自定义类型守卫函数
  4. 使用 never 检查完整性
← 返回 TypeScript Academy