Satisfies 运算符
一句话解释
satisfies 运算符 (x satisfies T) 检查那个值 matches 类型 T 不使用 widening 值's 类型, preserving 更多精确 literal 类型.
用大白话说
satisfies operator checks that a value conforms to a type without changing the value's inferred type. It is safer than type assertions for validation because it actually checks the type rather than just claiming it.
For example, const config = { port: 3000 } satisfies { port: number } verifies that config matches { port: number } but keeps the literal type 3000 instead of widening to number. This preserves more precise types for later use.
Use satisfies when you want to validate that an object matches an expected shape without losing the precise inferred types. It was added in TypeScript 4.9.
示例
type Colors = "red" | "green" | "blue";
const palette = {
primary: "red",
secondary: "blue",
} satisfies Record<string, Colors>;
// palette.primary has type "red" (not widened to Colors)satisfies 验证类型虽然 preserving literal 类型 'red'.
相关术语
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。