【发布时间】:2021-10-04 21:37:32
【问题描述】:
我正在尝试让 this example 像 this 一样工作:
interface Foo {
a: number;
b: string;
c: boolean;
}
type Explode<T> = keyof T extends infer K
? K extends unknown
? { [I in keyof T]: I extends K ? T[I] : never }
: never
: never;
type Test = Explode<Foo>;
const test: Test = {a: 1};
这给了我以下错误:
Type '{ a: number; }' is not assignable to type '{ a: number; b: never; c: never; } | { a: never; b: string; c: never; } | { a: never; b: never; c: boolean; }'.
Type '{ a: number; }' is missing the following properties from type '{ a: number; b: never; c: never; }': b, c
如何实例化Test 类型的对象而不出现错误?我想要一个可以包含字段 a 或 b 或 c 的类型(或为空的 {})。
【问题讨论】:
标签: typescript