Use union types instead of enums
See original GitHub issueDescription
Instead of generating enum types I would like to have an option to generate union types instead. If you like the idea I am very happy to implement this and create a PR.
Example
YAML model:
[...]
schemas:
Rating:
type: object
properties:
id:
type: string
value:
type: string
enum:
- negative
- neutral
- positive
Current behaviour
Generated model
export interface Rating {
id?: string;
value?: Rating.value;
}
export namespace Rating {
export enum value {
NEGATIVE = 'negative',
NEUTRAL = 'neutral',
POSITIVE = 'positive',
}
}
Usage
If I want to use this in my code I have to do this:
const exampleRating: Rating = {
id: '364363313',
value: Rating.value.NEUTRAL,
}
Proposed change
Generated model
export interface Rating {
id?: string;
value?: 'positive' | 'neutral' | 'negative'; // no enum anymore, instead we use union types
}
Usage
If I want to use this in my code I can do it much simpler now:
const exampleRating: Rating = {
id: '364363313',
value: 'neutral', // much simpler in my opinion, TS would still complain if I would use something that is not allowed
}
Explanation
Of course this would be behind a feature flag. At the beginning I thought useUnionTypes
is doing that, but it is not, so I have to find another name for my feature. I thought about useUnionTypesForEnums
, but I’m open for better suggestions.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:12 (8 by maintainers)
Top Results From Across the Web
Should You Use Enums or Union Types in Typescript?
In our perfectly fair comparison, union types get 6 points and enums get 3 points! This means using union types by default, and...
Read more >Typescript has unions, so are enums redundant?
With the recent versions of TypeScript, it is easy to declare iterable union types. Therefore, you should prefer union types to enums.
Read more >Union Types vs. Enums in TypeScript
Conceptually, enums can be a subset of union types with numeric or string values. Numeric enums are less safe. If you call updateStatus(20)...
Read more >The Difference Between TypeScript Unions, Enums, and Objects
Unlike enums, values in a union represent a set, which is a collection of unique values. So, if any values are repeated, then...
Read more >TypeScript | Union Types vs Enums
You will see a difference in the code size once TypeScript code is compiled into JavaScript code. Using string literal unions will “reduce”...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@KevinHerklotz @nandorojo there is a new version 0.5.0-alpha that should resolve this issue, if you have time them please do check. More info on the flag here: https://github.com/ferdikoomen/openapi-typescript-codegen#enums-vs-union-types---useuniontypes and https://github.com/ferdikoomen/openapi-typescript-codegen#babel-support
Thanks for testing. Just pushed 0.5.0 to NPM, closing this ticket.