Skip to content

Commit

Permalink
test: add cases for value-of-union ad readonly-keys-of-union
Browse files Browse the repository at this point in the history
  • Loading branch information
som-sm committed Dec 28, 2024
1 parent 729cc31 commit b20ac6c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test-d/internal/readonly-keys-of-union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {expectType} from 'tsd';
import type {ReadonlyKeysOfUnion} from '../../source/internal';

declare const test1: ReadonlyKeysOfUnion<{readonly a: 1; b: 2}>;
expectType<'a'>(test1);

declare const test2: ReadonlyKeysOfUnion<{readonly a: 1; b?: 2} | {readonly c?: 3; d: 4}>;
expectType<'a' | 'c'>(test2);

declare const test3: ReadonlyKeysOfUnion<{readonly a: 1; b?: 2} | {readonly c?: 3; d: 4} | {readonly c: 5} | {d: 6}>;
expectType<'a' | 'c'>(test3);

// Returns `never` if there's no readonly key
declare const test4: ReadonlyKeysOfUnion<{a: 1; b?: 2} | {c?: 3; d: 4}>;
expectType<never>(test4);

// Works with index signatures
declare const test5: ReadonlyKeysOfUnion<{readonly [x: string]: number; a: 1} | {readonly [x: symbol]: number; a: 2}>;
expectType<string | number | symbol>(test5);

// Works with arrays
declare const test7: ReadonlyKeysOfUnion<readonly string[] | readonly [number, number]>;
expectType<number | typeof Symbol.unscopables | '0' | '1' | 'length'>(test7);

// Works with functions
declare const test8: ReadonlyKeysOfUnion<(() => void) | {(): void; readonly a: 1}>;
expectType<'a'>(test8);
44 changes: 44 additions & 0 deletions test-d/internal/value-of-union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {expectType} from 'tsd';
import type {ValueOfUnion} from '../../source/internal';

// Works with objects
declare const test1: ValueOfUnion<{a: 1; b: 2} | {a: 3; c: 4}, 'a'>;
expectType<1 | 3>(test1);

// Works with arrays
declare const test2: ValueOfUnion<string[] | [number, boolean], number>;
expectType<string | number | boolean>(test2);

// Works with index signatures
declare const test3: ValueOfUnion<{[x: string]: string; a: 'a'} | {[x: number]: number; a: 'a'}, string>;
expectType<string>(test3);

declare const test4: ValueOfUnion<{[x: string]: string; a: 'a'} | {[x: number]: number; a: 'a'}, number>;
expectType<string | number>(test4);

// Works with functions
declare const test5: ValueOfUnion<(() => void) | {(): void; a: 1} | {(): void; a: 2}, 'a'>;
expectType<1 | 2>(test5);

// Ignores objects where `Key` is missing
declare const test6: ValueOfUnion<{a: 1; b: 2} | {a: 3; c: 4} | {a: 5; d: 6} | {e: 7}, 'a'>;
expectType<1 | 3 | 5>(test6);

// Adds `undefined` when the key is optional
declare const test7: ValueOfUnion<{readonly a?: 1; b: 2} | {a: 3; c: 4}, 'a'>;
expectType<1 | 3 | undefined>(test7);

// Works when `Key` is a union
declare const test8: ValueOfUnion<{a: 1; b: 2} | {a: 3; c: 4} | {a: 5; b: 6} | {e: 7}, 'a' | 'b'>;
expectType<1 | 2 | 3 | 5 | 6>(test8);

// @ts-expect-error - Errors if `Key` is missing from all of the objects
declare const test9: ValueOfUnion<{a: 1; b: 2} | {a: 3; c: 4}, 'd'>;

// Returns `any` when `Key` is `any`
declare const test10: ValueOfUnion<{a: 1; b: 2} | {a: 3; c: 4}, any>;
expectType<any>(test10);

// Returns `never` when `Key` is `never`
declare const test11: ValueOfUnion<{a: 1; b: 2} | {a: 3; c: 4}, never>;
expectType<never>(test11);

0 comments on commit b20ac6c

Please sign in to comment.