|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {ArrayFlat, PositiveInfinity} from '../index'; |
| 3 | + |
| 4 | +// Basic flattening tests |
| 5 | +expectType<ArrayFlat<[]>>([]); |
| 6 | +expectType<ArrayFlat<[1, 2, 3]>>([1, 2, 3]); |
| 7 | +expectType<ArrayFlat<[[1, 2], [3, 4]]>>([1, 2, 3, 4]); |
| 8 | +expectType<ArrayFlat<[1, [2, 3], 4]>>([1, 2, 3, 4]); |
| 9 | +expectType<ArrayFlat<[1, [2, [3]], 4]>>([1, 2, [3], 4]); |
| 10 | + |
| 11 | +// Test with explicit depth |
| 12 | +// eslint-disable-next-line unicorn/prevent-abbreviations |
| 13 | +type Arr = [[0, [1, [2, [3, [4, [5]]]]]]]; |
| 14 | +expectType<ArrayFlat<Arr, 0>>(null! as Arr); |
| 15 | +// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments |
| 16 | +expectType<ArrayFlat<Arr, 1>>([0, [1, [2, [3, [4, [5]]]]]]); |
| 17 | +expectType<ArrayFlat<Arr, 2>>([0, 1, [2, [3, [4, [5]]]]]); |
| 18 | +expectType<ArrayFlat<Arr, 3>>([0, 1, 2, [3, [4, [5]]]]); |
| 19 | +expectType<ArrayFlat<Arr, 4>>([0, 1, 2, 3, [4, [5]]]); |
| 20 | +expectType<ArrayFlat<Arr, 5>>([0, 1, 2, 3, 4, [5]]); |
| 21 | +expectType<ArrayFlat<Arr, 6>>([0, 1, 2, 3, 4, 5]); |
| 22 | +expectType<ArrayFlat<Arr, 7>>([0, 1, 2, 3, 4, 5]); |
| 23 | +expectType<ArrayFlat<Arr, PositiveInfinity>>([0, 1, 2, 3, 4, 5]); |
| 24 | + |
| 25 | +// Test with Infinity depth |
| 26 | +expectType<ArrayFlat<[1, [2, [3, [4]]]], PositiveInfinity>>([1, 2, 3, 4]); |
| 27 | +expectType<ArrayFlat<[[[[[1]]]]], PositiveInfinity>>([1]); |
| 28 | + |
| 29 | +// Test with different element types |
| 30 | +expectType<ArrayFlat<[string, [number, boolean]]>>([null! as string, null! as number, null! as boolean]); |
| 31 | +expectType<ArrayFlat<[{a: number}, [{b: string}]]>>([null! as {a: number}, null! as {b: string}]); |
| 32 | + |
| 33 | +// Test with union types |
| 34 | +expectType<ArrayFlat<[1, 2] | [[3, 4]]>>([1, 2] as [1, 2] | [3, 4]); |
| 35 | +expectType<ArrayFlat<[1, [2]] | [[3], 4]>>([1, 2] as [1, 2] | [3, 4]); |
| 36 | + |
| 37 | +// Test with rest elements |
| 38 | +expectType<ArrayFlat<[...Array<[number, string]>]>>([null! as number, null! as string]); |
| 39 | +expectType<ArrayFlat<[1, 2, ...Array<[string, boolean]>]>>([1, 2, null! as string, null! as boolean]); |
| 40 | +expectType<ArrayFlat<[1, [2, ...Array<3>], 4]>>([1, 2, ...(null! as Array<3>), 4]); |
| 41 | + |
| 42 | +// Test with mixed arrays and tuples |
| 43 | +expectType<ArrayFlat<[1, number[], 3]>>([1, ...(null! as number[]), 3]); |
| 44 | +expectType<ArrayFlat<[string, [number, ...boolean[]]]>>([null! as string, null! as number, ...(null! as boolean[])]); |
| 45 | + |
| 46 | +// Test with deeply nested structures |
| 47 | +expectType<ArrayFlat<[1, [2, [3, [4, [5]]]]], 3>>([1, 2, 3, 4, [5]]); |
| 48 | + |
| 49 | +// Test with readonly arrays |
| 50 | +expectType<ArrayFlat<readonly [1, readonly [2, 3]]>>([1, 2, 3]); |
| 51 | +expectType<ArrayFlat<readonly [readonly [1, 2], readonly [3, 4]]>>([1, 2, 3, 4]); |
| 52 | + |
| 53 | +// Edge cases |
| 54 | +expectType<ArrayFlat<Array<[]>>>([]); |
| 55 | +expectType<ArrayFlat<[[]]>>([]); |
| 56 | +expectType<ArrayFlat<[[], []]>>([]); |
| 57 | +expectType<ArrayFlat<undefined[]>>(null! as undefined[]); |
| 58 | +expectType<ArrayFlat<any[]>>(null! as any[]); |
| 59 | +expectType<ArrayFlat<unknown[]>>(null! as unknown[]); |
| 60 | +expectType<ArrayFlat<never[]>>(null! as never[]); |
0 commit comments