假设我有一个名为 Param 的类型,它等于,
type Param = {
field: string;
active: boolean;
}
Run Code Online (Sandbox Code Playgroud)
我用它来创建任何键(字符串)的记录,但其值是 Param 类型,例如,
const params: Record<string, Param> = {
foo: { field: 'hello', active: false },
boo: { field: 'world', active: true },
} as const
Run Code Online (Sandbox Code Playgroud)
为什么 TypeScript 无法推断paramsare的键'foo' | 'boo'?即使使用时as const。
params这是因为您已经显式设置了as的类型Record<string, Param>。相反,您必须让它是动态的,并对值类型添加一些强制措施。
type Param = {
field: string;
active: boolean;
}
const params = {
foo: { field: 'hello', active: false } as Param,
boo: { field: 'world', active: true } as Param,
}
// "foo" | "bar"
type paramsKeys = keyof typeof params
Run Code Online (Sandbox Code Playgroud)
如果你使用的是 typescript 4.9+,你可以这样做
const params = {
foo: { field: 'hello', active: false },
boo: { field: 'world', active: true },
} satisfies Record<string, Param>;
// "foo" | "bar"
type paramsKeys = keyof typeof params
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |