我知道keyof运算符,以及如何创建由该对象的键组成的联合类型,如下所示:
interface Foo {
a: string;
b: string;
}
type Goo = keyof Foo; // "a" | "b"我想做的正好相反,从键的联合创建一个新的对象类型。
const MakeTuple = <T extends string[]>(...args: T) => args;
const Types = MakeTuple("A", "B", "C");
type TypeVariant = typeof Types[number];
type VariantObject = {
// This gives error consider using a mapped object but I'm not sure how to do that
[type: TypeVariant]: [boolean, boolean, boolean, string[]];
}因此,我想要的是获取键的联合,并生成一个类型,该类型包含[boolean, boolean, boolean, string[]]类型的每个键的值。
发布于 2019-04-13 18:07:11
您可以只使用预定义的Record映射类型
const MakeTuple = <T extends string[]>(...args: T) => args;
const Types = MakeTuple("A", "B", "C");
type TypeVariant = typeof Types[number];
type VariantObject = Record< TypeVariant, [boolean, boolean, boolean, string[]] >https://stackoverflow.com/questions/55663811
复制相似问题