|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {HomomorphicPick} from '../../source/internal'; |
| 3 | + |
| 4 | +// Picks specified keys |
| 5 | +declare const test1: HomomorphicPick<{a: 1; b: 2; c: 3}, 'a' | 'b'>; |
| 6 | +expectType<{a: 1; b: 2}>(test1); |
| 7 | + |
| 8 | +// Works with unions |
| 9 | +declare const test2: HomomorphicPick<{a: 1; b: 2} | {a: 3; c: 4}, 'a'>; |
| 10 | +expectType<{a: 1} | {a: 3}>(test2); |
| 11 | + |
| 12 | +declare const test3: HomomorphicPick<{a: 1; b: 2} | {c: 3; d: 4}, 'a' | 'c'>; |
| 13 | +expectType<{a: 1} | {c: 3}>(test3); |
| 14 | + |
| 15 | +// Preserves property modifiers |
| 16 | +declare const test4: HomomorphicPick<{readonly a: 1; b?: 2; readonly c?: 3}, 'a' | 'c'>; |
| 17 | +expectType<{readonly a: 1; readonly c?: 3}>(test4); |
| 18 | + |
| 19 | +declare const test5: HomomorphicPick<{readonly a: 1; b?: 2} | {readonly c?: 3; d?: 4}, 'a' | 'c'>; |
| 20 | +expectType<{readonly a: 1} | {readonly c?: 3}>(test5); |
| 21 | + |
| 22 | +// Passes through primitives unchanged |
| 23 | +declare const test6: HomomorphicPick<string, never>; |
| 24 | +expectType<string>(test6); |
| 25 | + |
| 26 | +declare const test7: HomomorphicPick<number, never>; |
| 27 | +expectType<number>(test7); |
| 28 | + |
| 29 | +declare const test8: HomomorphicPick<boolean, never>; |
| 30 | +expectType<boolean>(test8); |
| 31 | + |
| 32 | +declare const test9: HomomorphicPick<bigint, never>; |
| 33 | +expectType<bigint>(test9); |
| 34 | + |
| 35 | +declare const test10: HomomorphicPick<symbol, never>; |
| 36 | +expectType<symbol>(test10); |
| 37 | + |
| 38 | +// Picks all keys, if `KeyType` is `any` |
| 39 | +declare const test11: HomomorphicPick<{readonly a: 1; b?: 2} | {readonly c?: 3}, any>; |
| 40 | +expectType<{readonly a: 1; b?: 2} | {readonly c?: 3}>(test11); |
| 41 | + |
| 42 | +// Picks no keys, if `KeyType` is `never` |
| 43 | +declare const test12: HomomorphicPick<{a: 1; b: 2}, never>; |
| 44 | +expectType<{}>(test12); |
| 45 | + |
| 46 | +// Works with index signatures |
| 47 | +declare const test13: HomomorphicPick<{[k: string]: unknown; a: 1; b: 2}, 'a' | 'b'>; |
| 48 | +expectType<{a: 1; b: 2}>(test13); |
| 49 | + |
| 50 | +// Doesn't pick `number` from a `string` index signature |
| 51 | +// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style |
| 52 | +declare const test14: HomomorphicPick<{[k: string]: unknown}, number>; |
| 53 | +expectType<{}>(test14); |
0 commit comments