TS抱怨foo中缺少b
,但我只需要其中的一些键。如何正确打字?
const shape = {a: 1, b:2}
const foo: Record<keyof typeof shape, any> = {
a: 42,
}
// Error: Property 'b' is missing in type '{ a: number; }' but required in type 'Record<"a" | "b", any>'.
TS抱怨foo中缺少b
,但我只需要其中的一些键。如何正确打字?
const shape = {a: 1, b:2}
const foo: Record<keyof typeof shape, any> = {
a: 42,
}
// Error: Property 'b' is missing in type '{ a: number; }' but required in type 'Record<"a" | "b", any>'.
1条答案
按热度按时间dhxwm5r41#
您应该使用内置的实用程序类型Partial,它将对象的所有字段转换为可选: