Typescript并集到交集返回值为never

Typescript并集到交集返回值为never,typescript,typescript-typings,Typescript,Typescript Typings,我的问题是关于这篇文章的 每当我将一个并集转换为交集时,我就放弃了并集类型,下面是我为解决这个问题而编写的一些代码 type SomeUnion = 'A' | 'B'; type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never type UnionToInterSectionWoNever<

我的问题是关于这篇文章的

每当我将一个并集转换为交集时,我就放弃了并集类型,下面是我为解决这个问题而编写的一些代码

type SomeUnion = 'A' | 'B';


type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never

type UnionToInterSectionWoNever<T> = {
  [K in keyof UnionToIntersection<T>]: UnionToIntersection<T>[K] extends never ? T[K] : UnionToIntersection<T>[K]
};


type UnionDistribution<T> = T extends SomeUnion ?
  { unionType: T } & (
    T extends 'A' ? { aProp1: string, aProp2: number } :
    T extends 'B' ? { bProp1: string } : never) :
  never;

type ABUnion = UnionDistribution<SomeUnion>;

type ABInterSection = UnionToIntersection<ABUnion>;

type ABInterSectionWoNever = UnionToInterSectionWoNever<ABUnion>;

// This in infered as never;
type ABInterSectionUnionType = ABInterSection['unionType'];

// This in inferred as 'A' | 'B'
type ABInterSectionWoNeverUnionType = ABInterSectionWoNever['unionType'];
键入SomeUnion='A'|'B';
输入UnionToIntersection=(U扩展任何?(k:U)=>void:never)扩展((k:inferi)=>void)?I:从来没有
类型UnionToInterSectionWoNever={
[K in-keyof UnionToIntersection]:UnionToIntersection[K]从不扩展?T[K]:UnionToIntersection[K]
};
类型UnionDistribution=T扩展了某些联合?
{unionType:T}&(
T扩展了'A'?{aProp1:string,aProp2:number}:
T扩展了“B”?{bProp1:string}:never):
从未;
ABUnion类型=联合分布;
ABInterSection类型=UnionToIntersection;
类型ABInterSectionWoNever=UnionToInterSectionWoNever;
//这被推断为从未发生过;
类型ABInterSectionUnionType=ABInterSection['unionType'];
//这可以推断为“A”|“B”
类型ABInterSectionWoNeverUnionType=ABInterSectionWoNever['unionType'];
因此,我对代码不是100%有信心,重新考虑一下同样的问题会非常有帮助。 我很好奇这样的事情何时会失败,以及如何解决同样的问题


提前感谢。

您将得到
永不
,因为TypeScript不能将类型表示为
'A'和'B'

看看这个:

型式试验={
福:“酒吧”,
} & {
foo:‘baz’,
}//从来没有
类型test2='A'和'B'//从不

这是在一个偶然的地方发现的。

我不明白重点。给定
A
B
类型,
UnionToIntersection
按预期返回
A&B
。无
从不
,无过度工程。很抱歉,可能我没有得到您想要得到的信息。“AbIntersection”中的类型“unionType”返回为never,后者则不是这样。
never
'A'和'B'的正确交集。
。你认为结果还应该是什么?为什么?