Typescript 将对象创建限制为 Record<string, T> 但随后获取定义的键?

rss*_*ncs 1 typescript

假设我有一个名为 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

Vig*_*aut 7

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)