在下面的代码中,需要什么 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>?
最佳答案
您可以反转 Y.value
的类型并将其用作 X
的参数:
type X<A> = { value: A };
type Y = X<number> | X<string>;
type Z = X<Y['value']>;
结果:
type Z = {
value: string | number;
};
关于typescript - 如何翻转 TypeScript 中联合的分布性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54986378/