@@ -339,40 +339,26 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
339
339
typeof schemaObject . maxItems === "number" && schemaObject . maxItems >= 0 && min <= schemaObject . maxItems
340
340
? schemaObject . maxItems
341
341
: undefined ;
342
- const estimateCodeSize = typeof max !== "number" ? min : ( max * ( max + 1 ) - min * ( min - 1 ) ) / 2 ;
342
+ const estimateCodeSize = max === undefined ? min : ( max * ( max + 1 ) - min * ( min - 1 ) ) / 2 ;
343
343
if (
344
344
options . ctx . arrayLength &&
345
345
( min !== 0 || max !== undefined ) &&
346
346
estimateCodeSize < 30 // "30" is an arbitrary number but roughly around when TS starts to struggle with tuple inference in practice
347
347
) {
348
- if ( min === max ) {
349
- const elements : ts . TypeNode [ ] = [ ] ;
350
- for ( let i = 0 ; i < min ; i ++ ) {
351
- elements . push ( itemType ) ;
352
- }
353
- return tsUnion ( [ ts . factory . createTupleTypeNode ( elements ) ] ) ;
354
- } else if ( ( schemaObject . maxItems as number ) > 0 ) {
355
- // if maxItems is set, then return a union of all permutations of possible tuple types
356
- const members : ts . TypeNode [ ] = [ ] ;
357
- // populate 1 short of min …
358
- for ( let i = 0 ; i <= ( max ?? 0 ) - min ; i ++ ) {
359
- const elements : ts . TypeNode [ ] = [ ] ;
360
- for ( let j = min ; j < i + min ; j ++ ) {
361
- elements . push ( itemType ) ;
362
- }
363
- members . push ( ts . factory . createTupleTypeNode ( elements ) ) ;
364
- }
365
- return tsUnion ( members ) ;
366
- }
367
348
// if maxItems not set, then return a simple tuple type the length of `min`
368
- else {
369
- const elements : ts . TypeNode [ ] = [ ] ;
370
- for ( let i = 0 ; i < min ; i ++ ) {
371
- elements . push ( itemType ) ;
372
- }
373
- elements . push ( ts . factory . createRestTypeNode ( ts . factory . createArrayTypeNode ( itemType ) ) ) ;
374
- return ts . factory . createTupleTypeNode ( elements ) ;
349
+ if ( max === undefined ) {
350
+ return ts . factory . createTupleTypeNode ( [
351
+ ...Array . from ( { length : min } ) . map ( ( ) => itemType ) ,
352
+ ts . factory . createRestTypeNode ( ts . factory . createArrayTypeNode ( itemType ) ) ,
353
+ ] ) ;
375
354
}
355
+
356
+ // if maxItems is set, then return a union of all permutations of possible tuple types
357
+ return tsUnion (
358
+ Array . from ( { length : max === undefined ? min : max - min + 1 } ) . map ( ( _ , index ) =>
359
+ ts . factory . createTupleTypeNode ( Array . from ( { length : index + min } ) . map ( ( ) => itemType ) ) ,
360
+ ) ,
361
+ ) ;
376
362
}
377
363
378
364
const finalType =
0 commit comments