Sha256: d33782b82eea0ee17b99ca563bd19b38259a3aaf096d306ceaf59cd4422629be

Contents?: true

Size: 1.31 KB

Versions: 89

Compression:

Stored size: 1.31 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
```
*/
export type MergeExclusive<FirstType, SecondType> =
	(FirstType | SecondType) extends object ?
		(Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) :
		FirstType | SecondType;

Version data entries

89 entries across 88 versions & 6 rubygems

Version Path
immosquare-cleaner-0.1.23 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.22 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.21 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.20 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.19 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.18 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.17 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.16 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.14 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.13 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.12 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.11 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.10 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.9 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.8 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.7 node_modules/type-fest/source/merge-exclusive.d.ts
immosquare-cleaner-0.1.6 node_modules/type-fest/source/merge-exclusive.d.ts
decidim-0.26.8 packages/eslint-config/node_modules/type-fest/source/merge-exclusive.d.ts
reed_sdk-1.0.1 node_modules/type-fest/source/merge-exclusive.d.ts
reed_sdk-1.0.0 node_modules/type-fest/source/merge-exclusive.d.ts