-
-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add cases for value-of-union and readonly-keys-of-union
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |