TypeScript Academy · 课时

const 断言与 as const

使用 as const 精确推断字面量类型和元组类型

第 4 / 4 课13 个步骤

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

什么是 as const

as const 是一种类型断言,它告诉 TypeScript 为表达式推断出可能的最具体字面量类型,并将所有属性设为只读。

const colors = ["red", "green", "blue"] as const;
// type: readonly ["red", "green", "blue"]

使用 as const 防止拓宽

没有 as const 时,数组和对象中的字符串值会拓宽为 string。使用它后,这些值会保持为字面量类型。

const dir1 = { direction: "left" };
// dir1.direction: string

const dir2 = { direction: "left" } as const;
// dir2.direction: "left"

对象字面量中的 as const

使用 as const 的对象中的每个属性都会变为 readonly,并递归地保留其字面量类型。

const theme = {
  color: "dark",
  sizes: { sm: 8, md: 16 },
} as const;
// theme.color: "dark", theme.sizes.sm: 8 (all readonly)

使用 as const 表示元组类型

使用 as const 的数组会变为带有字面量元素类型的只读元组,这对于定义固定的参数列表很有用。

const args = [1, "hello", true] as const;
// type: readonly [1, "hello", true]

从 as const 推导联合类型

一种常见模式是:定义一个由 as const 修饰的值数组,然后使用 typeof 和索引访问从中推导联合类型。

const STATUSES = ["pending", "active", "done"] as const;
type Status = typeof STATUSES[number];
// type Status = "pending" | "active" | "done"

函数参数中的 as const

调用函数时,可以内联使用 as const,让字面量类型在跨越调用边界时得以保留。

function setMode(mode: "dark" | "light") {}
setMode("dark" as const); // safe, preserves literal

只读递归

as const 会递归地应用 Readonly。嵌套对象和数组都会变为只读,从而防止深层变更。

const config = { db: { host: "localhost", port: 5432 } } as const;
// config.db.port = 5433; // Error: readonly

as const 与 Object.freeze

as const 是编译时断言,不提供运行时保护。Object.freeze 作用于运行时。两者承担互补的作用。

const obj = Object.freeze({ x: 1 }); // runtime immutability
const obj2 = { x: 1 } as const;      // compile-time literal types

常量断言与类型注解

类型注解会拓宽类型:const x: string = "hi" 的类型是 string。as const 会收窄类型:const x = "hi" as const 的类型是 "hi"。

const a: string = "hi"; // type: string
const b = "hi" as const; // type: "hi"

实际应用:枚举式对象

许多代码库使用带有 as const 的对象作为枚举的替代方案,既能避免枚举的缺陷,又能保留类型安全。

const Direction = { Up: "UP", Down: "DOWN" } as const;
type Direction = typeof Direction[keyof typeof Direction];
// "UP" | "DOWN"

回顾:常量断言

as const 会让 TypeScript 推断出字面量类型,并将所有属性视为只读。它对于从数组中派生精确的联合类型,以及构建安全的配置对象至关重要。

快速检查

as const 会对数组做什么?

您学到的内容

as const 是一种强大的工具,可用于固定字面量类型、创建只读结构,以及从数组中派生精确的联合类型。它是高级 TypeScript 模式的基石。

免费开始

用 AI 导师学习 TypeScript — 免费

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

课程
101
课程
352

常见问题解答

「const 断言与 as const」课时是免费的吗?

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

「const 断言与 as const」这节课中我会学到什么?

使用 as const 精确推断字面量类型和元组类型 你通过在浏览器中直接运行的动手代码来练习 TypeScript Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 TypeScript Academy 需要有经验吗?

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

「const 断言与 as const」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 类型扩展与收窄机制
  2. 上下文类型:从上下文推断
  3. 新鲜度与多余属性检查
  4. const 断言与 as const
← 返回 TypeScript Academy