Open Gelio opened 1 year ago
type IntersectionTypes<Types extends any[]> = Types extends [
infer T,
...infer O
]
? T extends Struct<any, any>
? Infer<T> & IntersectionTypes<O>
: unknown
: unknown;
export function intersection<Types extends [...StructAny[]]>(
...types: Types
): Type<IntersectionTypes<Types>, null> {
return ss.intersection(types as any) as any
}
I fixed with this.
Because
intersection
usesUnionToIntersection
on its arguments (except for the first one), it flattens anyunion
s that appear in theintersection
.TypeScript playground
Notice that when
baseUnion
is used as the first in theintersection
array, it works fine. WhenbaseUnion
appears second,UnionToIntesection
flattens it too much, which causes the finalunionAsSecondInIntersection
struct to benever
.Workaround
Use
union
as the first element of the array provided tointersection
.unionAsFirstInIntersection
is constructed correctly.