Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 从键的并集创建对象类型_Typescript - Fatal编程技术网

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