Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/union-to-intersection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type UnionToIntersection<Union> = (
// Infer the `Intersection` type since TypeScript represents the positional
// arguments of unions of functions as an intersection of the union.
) extends ((mergedIntersection: infer Intersection) => void)
// The `& Union` is to allow indexing by the resulting type
// The `& Union` is to ensure result of `UnionToIntersection<A | B>` is always assignable to `A | B`
? Intersection & Union
: never;

Expand Down
6 changes: 2 additions & 4 deletions test-d/union-to-intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ expectAssignable<{a: string; b: number}>(intersection1);
declare const intersection2: UnionToIntersection<{a: string} | {b: number} | {a: () => void}>;
expectAssignable<{a: string | (() => void); b: number}>(intersection2);

// It's possible to index by the resulting type.
type ObjectsUnion = {a: string; z: string} | {b: string; z: string} | {c: string; z: string};
declare const value: ObjectsUnion[UnionToIntersection<keyof ObjectsUnion>];
expectType<string>(value);
type Assignability<T, U, _K extends T | U> = never;
type TestAssignability<T, U> = Assignability<T, U, UnionToIntersection<T | U>>;