如何在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;
};