forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion-member.ts
More file actions
20 lines (16 loc) · 895 Bytes
/
Copy pathunion-member.ts
File metadata and controls
20 lines (16 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import {expectType} from 'tsd';
import type {UnionMember, IsNever} from '../index.d.ts';
// `UnionMember` distinguishes between different modifiers.
type UnionType = {a: 0} | {b: 0} | {a?: 0} | {readonly a?: 0} | {readonly a: 0};
expectType<true>({} as UnionMember<UnionType> extends UnionType ? true : false);
expectType<false>({} as UnionType extends UnionMember<UnionType> ? true : false);
// `never` acts as a termination condition with `IsNever`.
expectType<never>({} as UnionMember<never>);
expectType<unknown>({} as UnionMember<unknown>);
expectType<any>({} as UnionMember<any>);
// Ensure exactly one member is selected at a time, while covering all members in the union.
type UnionToTupleWithExclude<T, L = UnionMember<T>> =
IsNever<T> extends false
? UnionToTupleWithExclude<Exclude<T, L>> | [L]
: never;
expectType<[1] | [2] | [3]>({} as UnionToTupleWithExclude<1 | 2 | 3>);