Sha256: b00a6a38b27ac8da6d1f290fcf91821a0ea76eda17409a6b776584bbf323b928

Contents?: true

Size: 1.33 KB

Versions: 33

Compression:

Stored size: 1.33 KB

Contents

// Helper type. Not useful on its own.
type Without<FirstType, SecondType> = {[KeyType in Exclude<keyof FirstType, keyof SecondType>]?: never};

/**
Create a type that has mutually exclusive keys.

This type was inspired by [this comment](https://github.com/Microsoft/TypeScript/issues/14094#issuecomment-373782604).

This type works with a helper type, called `Without`. `Without<FirstType, SecondType>` produces a type that has only keys from `FirstType` which are not present on `SecondType` and sets the value type for these keys to `never`. This helper type is then used in `MergeExclusive` to remove keys from either `FirstType` or `SecondType`.

@example
```
import {MergeExclusive} from 'type-fest';

interface ExclusiveVariation1 {
	exclusive1: boolean;
}

interface ExclusiveVariation2 {
	exclusive2: string;
}

type ExclusiveOptions = MergeExclusive<ExclusiveVariation1, ExclusiveVariation2>;

let exclusiveOptions: ExclusiveOptions;

exclusiveOptions = {exclusive1: true};
//=> Works
exclusiveOptions = {exclusive2: 'hi'};
//=> Works
exclusiveOptions = {exclusive1: true, exclusive2: 'hi'};
//=> Error
```

@category Utilities
*/
export type MergeExclusive<FirstType, SecondType> =
	(FirstType | SecondType) extends object ?
		(Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) :
		FirstType | SecondType;

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.60 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.59 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.58 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.57 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.56 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.55 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.54 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.53 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.52 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.51 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.50 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.49 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.48 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.47 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.46 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.45 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.44 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.43 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.42 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.41 node_modules/crypto-random-string/node_modules/type-fest/source/merge-exclusive.d.ts