-
-
Couldn't load subscription status.
- Fork 259
Add support for non-empty array types with minItems constraint #2811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
💡 Enable Vercel Agent with $100 free credit for automated AI reviews |
|
@copilot there's already code for handling tuples, but I think it works only when both lower and upper bounds are defined. We'd be looking to make it work when only lower bounds are defined. This behavior should be automatic and we probably want to limit to something like minItems < 100, I think we already do something like this for tuples |
Co-authored-by: mrlubos <[email protected]>
Co-authored-by: mrlubos <[email protected]>
Overview
This PR adds support for generating TypeScript non-empty array tuple types when an OpenAPI schema defines an array with a
minItemsconstraint. This provides better type safety by ensuring arrays have at least the minimum required number of elements at compile time.Problem
Previously, arrays with
minItemsconstraints would generate standardArray<T>types, losing the valuable type information that the array must have at least N elements:Solution
Arrays with
minItems >= 1and nomaxItems(ormaxItems != minItems) now generate tuple types with required elements followed by a rest element:Type Generation Patterns
minItems: 0Array<T>minItems: 1[T, ...Array<T>]minItems: 3[T, T, T, ...Array<T>]minItems: 2, maxItems: 2[T, T](fixed-size, existing behavior)minItems: 2, maxItems: 5Array<T>(bounded arrays use regular type)minItems: 101Array<T>(exceeds safety limit of 100)Implementation Details
Core Changes
tsc/types.ts: AddedcreateRestTypeNodehelper to create TypeScript rest type nodes for tuple spread elementstsc/typedef.ts: AddedcreateNonEmptyArrayTupleNodefunction that generates tuple types with required elements + rest elementplugins/@hey-api/typescript/plugin.ts: UpdatedarrayTypeToIdentifierto check forminItemsconstraint and generate non-empty tuple types when appropriateutils/type.ts: UpdatedtypeArrayfor backward compatibility with legacy parserSafety Considerations
minItems <= 100to avoid generating excessively long type definitionsminItems/maxItemsTesting
specs/3.1.x/min-items-array.yamlExample Usage
Given this OpenAPI specification:
The generated TypeScript type will now be:
This provides compile-time safety ensuring the array has at least one element when provided.
Benefits
minItemsconstraintminItems, existing code unchangedFixes #[issue-number]
Original prompt
Fixes #2810
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.