question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use union types instead of enums

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
ferdikoomencommented, Sep 27, 2020

@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

1reaction
ferdikoomencommented, Oct 3, 2020

Thanks for testing. Just pushed 0.5.0 to NPM, closing this ticket.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found