Sha256: 55480aa69f3984607fa60b3862b5cd24c2ee7bdd4edaed1eef6a8b46554e947f

Contents?: true

Size: 1.92 KB

Versions: 62

Compression:

Stored size: 1.92 KB

Contents

/**
Convert 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 [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).

@example
```
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 its way into your library code follows.

@example
```
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 unknown` 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 unknown
		// 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;

Version data entries

62 entries across 62 versions & 5 rubygems

Version Path
trusty-cms-6.3.1 node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.26 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/ansi-escapes/node_modules/type-fest/source/union-to-intersection.d.ts