-
-
Notifications
You must be signed in to change notification settings - Fork 528
Enhancing openapi-typescript for Improved Type Safety: Generating Arrays from Enum Definitions #1616
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
Comments
That would be very helpful! We validate our forms with Zod to ensure that the form values match the OpenAPI spec. If the spec provides an enum with valid values, I want to pass those values to |
I’d be open to this! While it’s still a library goal to have no runtime, technically TypeScript enums do have a runtime, so that line has been crossed already, though we do want to be reserved in what we generate (because this library tries to steer away from runtime codegen). If anyone wants to open a PR for this, I’d like to see:
If all 3 criteria are satisfied, happy to ship it 🙂. Just note that the current |
I started working on it and managed to emit this in export const personGenderValues: components["schemas"]["person"]["gender"][] = ["male", "female", "unknown"]; I also put it behind an optional CLI flag:
Not sure about the naming and description though. I'll create a PR next week, but I have to clean up my code first and figure add tests for it. |
It would be nice to backport this to 6.x |
Unfortunately I do not have time in the immediate future to add this, but I would love a PR if someone else is able to! |
Does the new |
It wouldn't be necessary in the first place if we had this because getting enum values would be as easy as |
Is there a plan to support Here is an example export type ObjectValues<T> = T[keyof T];
export const LAND_INTERACTION_MODE = {
Together: 'together',
Alone: 'alone',
NoPreference: 'no_preference',
TogetherButNotAllTime: 'together_but_not_all_time',
} as const;
export type LandInteractionMode = ObjectValues<typeof LAND_INTERACTION_MODE>;
export const LAND_INTERACTION_MODES = Object.values(LAND_INTERACTION_MODE); Instead of export enum LandProposalJsonldPreferredInteractionMode {
alone = "alone",
together = "together",
together_but_not_all_time = "together_but_not_all_time",
no_preference = "no_preference"
} |
Description
I'm utilizing openapi-typescript to enhance API type safety. However, when parsing parameters using user-defined type guards, it's necessary to validate the elements of a Union type, requiring an array of Enum values. Currently, openapi-typescript only generates Union types, necessitating manual array definition, which introduces redundancy.
Proposal
I propose adding an option to openapi-typescript output to generate an array of Enum values. This would allow for the simultaneous generation of Union types and arrays, improving type safety and reducing code redundancy. For instance, consider the following option:
Checklist
The text was updated successfully, but these errors were encountered: