import type {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
Create a type that has mutually exclusive keys.
This type was inspired by this comment.
This type works with a helper type, called
Without.Without<FirstType, SecondType>produces a type that has only keys fromFirstTypewhich are not present onSecondTypeand sets the value type for these keys tonever. This helper type is then used inMergeExclusiveto remove keys from eitherFirstTypeorSecondType.