Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
A helpful utility which maps a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types). | ||
Inspired by a [stack overflow answer](https://stackoverflow.com/a/50375286/2172153). | ||
@example | ||
```ts | ||
import {UnionToIntersection} from 'type-fest'; | ||
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean}; | ||
type Intersection = UnionToIntersection<Union>; | ||
//=> {the(): void; great(arg: string): void; escape: boolean}; | ||
``` | ||
A more applicable example which could make it's way into your library code follows. | ||
@example | ||
```ts | ||
import {UnionToIntersection} from 'type-fest'; | ||
class CommandOne { | ||
commands: { | ||
a1: () => undefined, | ||
b1: () => undefined, | ||
} | ||
} | ||
class CommandTwo { | ||
commands: { | ||
a2: (argA: string) => undefined, | ||
b2: (argB: string) => undefined, | ||
} | ||
} | ||
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands) | ||
type Union = typeof union; | ||
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void} | ||
type Intersection = UnionToIntersection<Union>; | ||
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void} | ||
``` | ||
*/ | ||
export type UnionToIntersection<Union> = ( | ||
// `extends any` is always going to be the case and is used to convert the | ||
// `Union` into a [distributive conditional | ||
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types). | ||
Union extends any | ||
// The union type is used as the only argument to a function since the union | ||
// of function arguments is an intersection. | ||
? (distributedUnion: Union) => void | ||
// This won't happen. | ||
: never | ||
// 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) | ||
? Intersection | ||
: never; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {expectType} from 'tsd'; | ||
import {UnionToIntersection} from '..'; | ||
|
||
declare const intersection1: UnionToIntersection<{a: string} | {b: number}>; | ||
expectType<{a: string; b: number}>(intersection1); | ||
|
||
// Creates a union of matching properties. | ||
declare const intersection2: UnionToIntersection<{a: string} | {b: number} | {a: () => void}>; | ||
expectType<{a: string | (() => void); b: number}>(intersection2); |