【发布时间】:2019-04-13 19:38:01
【问题描述】:
我知道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[]] 类型的每个键的值。
【问题讨论】:
标签: typescript