Skip to content

Commit 1c642f4

Browse files
test: fix readonly case, rename unused test cases
1 parent 4ced771 commit 1c642f4

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

test-d/union-member.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import {expectType} from 'tsd';
2-
import type {UnionMember, IsNever} from '../index.d.ts';
2+
import type {UnionMember, IsNever, IsEqual} from '../index.d.ts';
33

4-
// `UnionMember` distinguishes between different modifiers.
5-
type UnionType = {a: 0} | {b: 0} | {a?: 0} | {readonly a?: 0} | {readonly a: 0};
6-
expectType<true>({} as UnionMember<UnionType> extends UnionType ? true : false);
7-
expectType<false>({} as UnionType extends UnionMember<UnionType> ? true : false);
4+
type UnionType = {a: 0} | {readonly a: 0};
5+
type PickedUnionMember = UnionMember<UnionType>;
6+
expectType<true>({} as PickedUnionMember extends UnionType ? true : false);
7+
expectType<false>({} as IsEqual<UnionType, PickedUnionMember>);
88

99
// `never` acts as a termination condition with `IsNever`.
1010
expectType<never>({} as UnionMember<never>);
1111

1212
expectType<unknown>({} as UnionMember<unknown>);
1313
expectType<any>({} as UnionMember<any>);
1414

15-
// Ensure exactly one member is selected at a time, while covering all members in the union.
16-
type UnionToTupleWithExclude<T, L = UnionMember<T>> =
15+
// `WrapMemberInTuple` ensures `UnionMember` selects exactly one member at a time.
16+
type WrapMemberInTuple<T, L = UnionMember<T>> =
1717
IsNever<T> extends false
18-
? UnionToTupleWithExclude<Exclude<T, L>> | [L]
18+
? WrapMemberInTuple<Exclude<T, L>> | [L]
1919
: never;
20-
expectType<[1] | [2] | [3]>({} as UnionToTupleWithExclude<1 | 2 | 3>);
20+
expectType<[1] | [2] | [3]>({} as WrapMemberInTuple<1 | 2 | 3>);
21+
expectType<['foo'] | ['bar'] | ['baz']>({} as WrapMemberInTuple<'foo' | 'bar' | 'baz'>);
22+
expectType<[1] | ['foo'] | [true] | [100n] | [null] | [undefined]>(
23+
{} as WrapMemberInTuple<1 | 'foo' | true | 100n | null | undefined>,
24+
);
25+
expectType<[{a: string}] | [{b: number}]>({} as WrapMemberInTuple<{a: string} | {b: number}>);

0 commit comments

Comments
 (0)