阅读摘录
用 satisfies 约束配置对象
以前写配置文件,习惯用类型标注:
const config: Record<string, string | string[]> = { ... };
代价是所有键的字面量类型都丢了,后面 config.port 拿不到精确类型。
换成 satisfies 就能两全:
const config = {
host: "localhost",
origins: ["a.com", "b.com"],
} satisfies Record<string, string | string[]>;
既做了校验,又保住了每个键推断出来的具体类型。