Typescript 从键的并集创建对象类型
我知道Typescript 从键的并集创建对象类型,typescript,Typescript,我知道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
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[]];
}
constmaketuple=(…args:T)=>args;
常量类型=生成组(“A”、“B”、“C”);
类型变量=类型的类型[数量];
类型VariantObject={
//这使用映射对象给出了错误考虑,但我不知道该怎么做。
[type:TypeVariant]:[boolean,boolean,boolean,string[];
}
因此,我想要的是获得一个键的并集并生成一个类型,该类型包含
[布尔、布尔、布尔、字符串[]]
类型的每个键的一个值,您可以使用预定义的记录
映射类型
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[]] >
constmaketuple=(…args:T)=>args;
常量类型=生成组(“A”、“B”、“C”);
类型变量=类型的类型[数量];
type VariantObject=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[]] >
constmaketuple=(…args:T)=>args;
常量类型=生成组(“A”、“B”、“C”);
类型变量=类型的类型[数量];
type VariantObject=Record