Code Monkey home page Code Monkey logo

Comments (6)

Wtiben avatar Wtiben commented on May 29, 2024

https://blog.graphqleditor.com/enums-are-still-bad

from orval.

Sergey-Ubogov avatar Sergey-Ubogov commented on May 29, 2024

@Wtiben, the article is misleading, because it says:
Essentially we're getting everything we would from an enum, without the drawbacks of actually using an enum.
and
key issue of ‘why not just use a const’ will remain for those who find them entirely redundant.

I gave the answer to their question above, but I'll duplicate it here for you. With a constant, you will get a non-working code:

export type MyEnum = typeof MyEnum[keyof typeof MyEnum];
export const MyEnum = {
    testA: 'testA',
    testB: 'testB',
    testC: 'testC',
} as const;

const a = MyEnum.testA;
const arr = [MyEnum.testB, MyEnum.testC];
arr.includes(a); // TS2345: Argument of type '"testA"' is not assignable to parameter of type '"testB" | "testC"'. 

At the same time, with enum you will get a working and more concise code:

export enum MyEnum {
    testA = 'testA',
    testB = 'testB',
    testC = 'testC',
}

const a = MyEnum.testA;
const arr = [MyEnum.testB, MyEnum.testC];
arr.includes(a);

Besides, I don't insist on generating enum by default. this can be done by some flag in the config

from orval.

melloware avatar melloware commented on May 29, 2024

PR is welcome!

from orval.

dsthode avatar dsthode commented on May 29, 2024

Hey, I've taken up the gauntlet and contributed my first PR to the project with an optional configuration parameter to generate native Typescript enums and the code to handle it.

from orval.

melloware avatar melloware commented on May 29, 2024

@dsthode sent you 1 review comment

from orval.

dsthode avatar dsthode commented on May 29, 2024

@melloware thank you for the comment, I've performed the changes requested.

from orval.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.