1
- import { expectType } from 'tsd' ;
1
+ import { expectAssignable , expectType } from 'tsd' ;
2
2
import type { ArrayFlat , PositiveInfinity } from '../index' ;
3
3
4
4
// Basic flattening tests
@@ -34,9 +34,7 @@ expectType<ArrayFlat<[{a: number}, [{b: string}]]>>([null! as {a: number}, null!
34
34
expectType < ArrayFlat < [ 1 , 2 ] | [ [ 3 , 4 ] ] > > ( [ 1 , 2 ] as [ 1 , 2 ] | [ 3 , 4 ] ) ;
35
35
expectType < ArrayFlat < [ 1 , [ 2 ] ] | [ [ 3 ] , 4 ] > > ( [ 1 , 2 ] as [ 1 , 2 ] | [ 3 , 4 ] ) ;
36
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 ] ) ;
37
+ expectType < ArrayFlat < [ ...Array < [ number , string ] > ] > > ( null ! as [ number ?, string ?, number ?, string ?, number ?, string ?, number ?, string ?, number ?, string ?, number ?, string ?, number ?, string ?, number ?, string ?, number ?, string ?, number ?, string ?] ) ;
40
38
expectType < ArrayFlat < [ 1 , [ 2 , ...Array < 3 > ] , 4 ] > > ( [ 1 , 2 , ...( null ! as Array < 3 > ) , 4 ] ) ;
41
39
42
40
// Test with mixed arrays and tuples
@@ -55,6 +53,49 @@ expectType<ArrayFlat<Array<[]>>>([]);
55
53
expectType < ArrayFlat < [ [ ] ] > > ( [ ] ) ;
56
54
expectType < ArrayFlat < [ [ ] , [ ] ] > > ( [ ] ) ;
57
55
expectType < ArrayFlat < undefined [ ] > > ( null ! as undefined [ ] ) ;
58
- expectType < ArrayFlat < any [ ] > > ( null ! as any [ ] ) ;
59
56
expectType < ArrayFlat < unknown [ ] > > ( null ! as unknown [ ] ) ;
60
- expectType < ArrayFlat < never [ ] > > ( null ! as never [ ] ) ;
57
+
58
+ // Test specifically for non-tuple array handling (Array<T> vs [T, T])
59
+ type GenericNumberArrays = number [ ] [ ] ;
60
+ expectAssignable < ArrayFlat < GenericNumberArrays > > ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
61
+
62
+ // Test for deeply nested non-tuple arrays with specific depths
63
+ type DeepGenericArray = number [ ] [ ] [ ] [ ] ;
64
+ expectAssignable < ArrayFlat < DeepGenericArray , 2 > > ( [ [ 1 , 2 ] , [ 3 , 4 ] ] ) ;
65
+ expectAssignable < ArrayFlat < DeepGenericArray , 3 > > ( [ 1 , 2 , 3 , 4 ] ) ;
66
+
67
+ // Test for mixed generic arrays and tuples
68
+ type MixedGenericAndTuple = Array < [ number , string ] > ;
69
+ expectAssignable < ArrayFlat < MixedGenericAndTuple > > ( [ 1 , 'a' , 2 , 'b' ] ) ;
70
+
71
+ // Test for array with optional elements in nested structure
72
+ type NestedOptional = [ number , Array < [ string ?, number ?] > ] ;
73
+ expectAssignable < ArrayFlat < NestedOptional , 2 > > ( [ 1 , 'a' , 2 , 'b' , 3 ] ) ;
74
+
75
+ // Test for arrays with rest elements in nested structure
76
+ type NestedRest = [ string , Array < [ ...number [ ] ] > ] ;
77
+ expectAssignable < ArrayFlat < NestedRest , 2 > > ( [ 'a' , 1 , 2 , 3 , 4 , 5 ] ) ;
78
+
79
+ // Test for flattening arrays with union types in nested structures
80
+ type NestedUnion = Array < Array < string | number > > ;
81
+ expectAssignable < ArrayFlat < NestedUnion > > ( [ 'a' , 1 , 'b' , 2 ] ) ;
82
+
83
+ // Test for empty array in complex structure
84
+ type ComplexWithEmpty = [ number , Array < [ ] > , string ] ;
85
+ expectAssignable < ArrayFlat < ComplexWithEmpty > > ( [ 1 , 'string' ] ) ;
86
+
87
+ // Test for array with undefined/null elements
88
+ type ArrayWithNullish = [ number , [ undefined , null ] ] ;
89
+ expectAssignable < ArrayFlat < ArrayWithNullish > > ( [ 1 , undefined , null ] ) ;
90
+
91
+ // Test for array with mixed depth elements
92
+ type MixedDepthArray = [ number , string [ ] , [ [ boolean ] ] ] ;
93
+ expectAssignable < ArrayFlat < MixedDepthArray > > ( [ 1 , 'a' , 'b' , [ true ] ] ) ;
94
+
95
+ // Test for readonly nested arrays with different depths
96
+ type ReadonlyNestedComplex = readonly [ number , ReadonlyArray < readonly string [ ] > ] ;
97
+ expectAssignable < ArrayFlat < ReadonlyNestedComplex , 2 > > ( [ 1 , 'a' , 'b' , 'c' ] ) ;
98
+
99
+ // Test for recursive flattening with non-array elements
100
+ type RecursiveWithNonArray = [ number , [ string , { a : number } ] ] ;
101
+ expectAssignable < ArrayFlat < RecursiveWithNonArray > > ( [ 1 , 'string' , { a : 42 } ] ) ;
0 commit comments