Skip to main content
Module

x/scaffold/src/deps/types.ts>MergeExclusive

scaffold your next project with style and 💗
Latest
type alias MergeExclusive
import { type MergeExclusive } from "https://deno.land/x/scaffold@0.3.0/src/deps/types.ts";

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 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.

Examples

Example 1

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

Type Parameters

FirstType
SecondType
definition: (FirstType | SecondType) extends object ? (Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) : FirstType | SecondType