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
36 lines (31 loc) · 1.78 KB
/
union-member.ts
File metadata and controls
36 lines (31 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import {expectType} from 'tsd';
import type {UnionMember, IsNever, IsEqual} from '../index.d.ts';
expectType<false>({} as boolean extends UnionMember<boolean> ? true : false);
expectType<false>({} as (1 | 'foo' | 'bar') extends UnionMember<1 | 'foo' | 'bar'> ? true : false);
expectType<false>({} as ({foo: string} | {bar: number}) extends UnionMember<{foo: string} | {bar: number}> ? true : false);
expectType<number>({} as UnionMember<number>);
expectType<string>({} as UnionMember<string>);
expectType<bigint>({} as UnionMember<bigint>);
expectType<true>({} as UnionMember<true>);
expectType<false>({} as UnionMember<false>);
expectType<null>({} as any as UnionMember<null>);
expectType<undefined>({} as any as UnionMember<undefined>);
expectType<never>({} as UnionMember<never>);
expectType<unknown>({} as UnionMember<unknown>);
expectType<any>({} as UnionMember<any>);
// `WrapMemberInTuple` ensures `UnionMember` selects exactly one member at a time.
type WrapMemberInTuple<T, L = UnionMember<T>> =
IsNever<T> extends false
? WrapMemberInTuple<Exclude<T, L>> | [L]
: never;
expectType<[1] | [2] | [3]>({} as WrapMemberInTuple<1 | 2 | 3>);
expectType<['foo'] | ['bar'] | ['baz']>({} as WrapMemberInTuple<'foo' | 'bar' | 'baz'>);
expectType<[1] | ['foo'] | [true] | [100n] | [null] | [undefined]>(
{} as WrapMemberInTuple<1 | 'foo' | true | 100n | null | undefined>,
);
expectType<[{a: string}] | [{b: number}]>({} as WrapMemberInTuple<{a: string} | {b: number}>);
type UnionType = {a: 0} | {readonly a: 0};
type PickedUnionMember = UnionMember<UnionType>;
// We can't use `UnionType extends PickedUnionMember ? true : false` for testing here,
// because that would always be `true` as `UnionType` extends both of its members individually.
expectType<false>({} as IsEqual<UnionType, PickedUnionMember>);