|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {SplitOnRestElement} from '../index.d.ts'; |
| 3 | + |
| 4 | +// Fixed tuples (No rest element) |
| 5 | +expectType<SplitOnRestElement<[]>>({} as [[], [], []]); |
| 6 | +expectType<SplitOnRestElement<[1]>>({} as [[1], [], []]); |
| 7 | +expectType<SplitOnRestElement<[1, 2, 3]>>({} as [[1, 2, 3], [], []]); |
| 8 | +expectType<SplitOnRestElement<readonly ['a', 'b', 'c']>>({} as readonly [['a', 'b', 'c'], [], []]); |
| 9 | + |
| 10 | +// Rest elements (true variadic) |
| 11 | +expectType<SplitOnRestElement<[1, ...number[]]>>({} as [[1], number[], []]); |
| 12 | +expectType<SplitOnRestElement<[...string[]]>>({} as [[], string[], []]); |
| 13 | +expectType<SplitOnRestElement<[...never[]]>>({} as [[], never[], []]); |
| 14 | +expectType<SplitOnRestElement<[1, ...number[], 2]>>({} as [[1], number[], [2]]); |
| 15 | +expectType<SplitOnRestElement<['a', ...string[], 'b']>>({} as [['a'], string[], ['b']]); |
| 16 | +expectType<SplitOnRestElement<[...boolean[], string]>>({} as [[], boolean[], [string]]); |
| 17 | +expectType<SplitOnRestElement<[...string[], 'x', 'y']>>({} as [[], string[], ['x', 'y']]); |
| 18 | +expectType<SplitOnRestElement<[...never[], 1]>>({} as [[], never[], [1]]); |
| 19 | +expectType<SplitOnRestElement<[undefined, ...boolean[], null]>>({} as [[undefined], boolean[], [null]]); |
| 20 | +expectType<SplitOnRestElement<[void, ...never[], 1]>>({} as [[void], never[], [1]]); |
| 21 | +expectType<SplitOnRestElement<[null, ...any[], null]>>({} as [[null], any[], [null]]); |
| 22 | +expectType<SplitOnRestElement<[...Array<{id: string}>, number]>>({} as [[], Array<{id: string}>, [number]]); |
| 23 | +expectType<SplitOnRestElement<[1, ...Array<readonly [string, number]>, 2]>>({} as [[1], Array<readonly [string, number]>, [2]]); |
| 24 | + |
| 25 | +// Generic arrays |
| 26 | +expectType<SplitOnRestElement<string[]>>({} as [[], string[], []]); |
| 27 | +expectType<SplitOnRestElement<number[]>>({} as [[], number[], []]); |
| 28 | +expectType<SplitOnRestElement<unknown[]>>({} as [[], unknown[], []]); |
| 29 | +expectType<SplitOnRestElement<any[]>>({} as [[], any[], []]); |
| 30 | + |
| 31 | +// Unions |
| 32 | +expectType<SplitOnRestElement<[...Array<string | number>, boolean]>>({} as [[], Array<string | number>, [boolean]]); |
| 33 | +expectType<SplitOnRestElement<[1, 2] | [3, 4]>>({} as [[1, 2], [], []] | [[3, 4], [], []]); |
| 34 | +expectType<SplitOnRestElement<[1, ...number[]] | ['foo', ...string[]]>>({} as [[1], number[], []] | [['foo'], string[], []]); |
| 35 | + |
| 36 | +// Preserve optional |
| 37 | +expectType<SplitOnRestElement<[0, 1?, 2?, ...never[]]>>({} as [[0, 1?, 2?], never[], []]); |
| 38 | +expectType<SplitOnRestElement<[number?, ...string[]]>>({} as [[number?], string[], []]); |
| 39 | +expectType<SplitOnRestElement<[number, boolean?, ...string[]]>>({} as [[number, boolean?], string[], []]); |
| 40 | + |
| 41 | +// Remove optional |
| 42 | +expectType<SplitOnRestElement<[0, 1?, 2?, ...never[]], {preserveOptionalModifier: false}>>({} as [[0, 1, 2], never[], []]); |
| 43 | +expectType<SplitOnRestElement<[number?, ...string[]], {preserveOptionalModifier: false}>>({} as [[number], string[], []]); |
| 44 | +expectType<SplitOnRestElement<[number, boolean?, ...string[]], {preserveOptionalModifier: false}>>({} as [[number, boolean], string[], []]); |
| 45 | + |
| 46 | +// Readonly |
| 47 | +expectType<SplitOnRestElement<readonly []>>({} as readonly [[], [], []]); |
| 48 | +expectType<SplitOnRestElement<readonly [number] | [string]>>({} as readonly [[number], [], []] | [[string], [], []]); |
| 49 | +expectType<SplitOnRestElement<readonly [...number[], 2]>>({} as readonly [[], number[], [2]]); |
| 50 | +expectType<SplitOnRestElement<readonly [1, ...string[], 2] | readonly ['foo'?, ...string[]]>>({} as readonly [[1], string[], [2]] | readonly [['foo'?], string[], []]); |
| 51 | +expectType<SplitOnRestElement<readonly [1, 2, 3]>>({} as readonly [[1, 2, 3], [], []]); |
| 52 | + |
| 53 | +// Edge: `never` / `any` |
| 54 | +expectType<SplitOnRestElement<any>>({} as any); |
| 55 | +expectType<SplitOnRestElement<never>>({} as never); |
0 commit comments