Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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,在下面的代码中,需要什么SomeMagic类型来反转Y的分布 type X<A> = { value: A }; type Y = X<number> | X<string>; type Z = SomeMagic<Y>; // <-- what SomeMagic should be to get Z of X<number | string>? type X={value:A}; Y型=X | X; 类型Z=SomeMag

在下面的代码中,需要什么
SomeMagic
类型来反转
Y
的分布

type X<A> = { value: A };
type Y = X<number> | X<string>;
type Z = SomeMagic<Y>;  // <-- what SomeMagic should be to get Z of X<number | string>?
type X={value:A};
Y型=X | X;

类型Z=SomeMagic;// 您可以反转
Y.value
的类型,并将其用作
X
的参数:

type X<A> = { value: A };
type Y = X<number> | X<string>;
type Z = X<Y['value']>;

或者,如果
X
更复杂,您可以使用条件类型来提取
a
类型GetA=T扩展X?U:从来没有
type Z = {
  value: string | number;
};