Skip to content

feat: generate tuple type for fixed-length arrays (min equals max)#231

Open
2wheeh wants to merge 1 commit into
ThomasAribart:mainfrom
2wheeh:feat/equal-to-tuple
Open

feat: generate tuple type for fixed-length arrays (min equals max)#231
2wheeh wants to merge 1 commit into
ThomasAribart:mainfrom
2wheeh:feat/equal-to-tuple

Conversation

@2wheeh

@2wheeh 2wheeh commented Apr 9, 2026

Copy link
Copy Markdown

Description

When an array schema has minItems === maxItems, generate a tuple type instead of T[].

// Before
FromSchema<{ type: "array"; items: { type: "string" }; minItems: 2; maxItems: 2 }>
// => string[]

// After
// => [string, string]

This PR only handles the equal case (minItems === maxItems).

It seems Range support (minItems !== maxItems → union of tuple lengths) is easily possible by reusing the existing ApplyMinMax pipeline with Repeat, but left out intentionally due to:

  • Performance concerns with large ranges (deep type-level recursion)
  • Need a disscussion for a code size guard (similar to openapi-typescript's estimateCodeSize < 30)

Changes

  • Added Repeat<T, N> type utility that builds a fixed-length tuple of repeated type
  • Added ParseArraySchema in array.ts to check minItems === maxItems and produce M.$Tuple via Repeat
  • Falls back to M.$Array (existing behavior) for all other cases

Breaking change

This is technically a narrowing change: string[][string, string]. Code that assigns a general T[] to a FromSchema type with fixed-length constraints may break. The type is now more correct but stricter — may warrant a minor version bump.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant