STOP using Enums | Typescript

Поділитися
Вставка
  • Опубліковано 20 січ 2025

КОМЕНТАРІ • 34

  • @Computeshorts
    @Computeshorts  Рік тому +3

    Will you stop using enums after this video?

    • @maran2301
      @maran2301 Рік тому +3

      Just because Enums are bad in type/javascript doesn't mean there are bad at every other language. In fact, pretty much every language implements Enums much better.

    • @Computeshorts
      @Computeshorts  Рік тому

      @maran2347 I was focusing on typescript in the video in pretty much every other language they are super useful I'll adjust the title to stop the confusion

    • @adriankal
      @adriankal Рік тому +3

      After watching I would immediately stop using typescript. I'm glad I made this decision 3 years ago.

    • @Tekay37
      @Tekay37 Рік тому +2

      I will stop using TypeScript instead.

    • @Computeshorts
      @Computeshorts  Рік тому

      @@Tekay37 lol 😆

  • @noideaprojects
    @noideaprojects Рік тому +37

    The point of enums, in any language, is to avoid people passing strings around. Problematic for refactoring or long term maintenance in any even moderate sized code bases.

    • @ck-dl4to
      @ck-dl4to Рік тому +1

      enum is just great

  • @willevans3533
    @willevans3533 Рік тому +29

    What’s wrong with imports? Also, doesn’t passing the string defeat the point of using an enum

  • @spaghettiking653
    @spaghettiking653 Рік тому +6

    Why do want to use strings instead of just referring to the enum name for every time you want to access a member?

  • @TheMMMedo
    @TheMMMedo Рік тому +11

    For me, enums are definitely better than to pass strings around. Refactoring is a lot easier - how do you refactor a code when you pass around plain strings? Lets say, your "warning" log level becomes "warn" for some reason. You go through full codebase and change it by hand where needed / where typescript yells at you? Yeah, no, thanks.
    It has some caveats, for sure, but still looks like a lot better option for me.

  • @martygusto3056
    @martygusto3056 Рік тому +7

    why would you use a string when you already have defined a name? isn't this just like 1 step forward two steps back? although the conversion by typescript is weird but what you did in the last example using a simple object could have been used as Levels.DEBUG.

    • @Computeshorts
      @Computeshorts  Рік тому

      The fact that you can use both string and the object itself is where the power comes from rather than being limited to just using the enum itself

    • @yxngboypolo
      @yxngboypolo 7 місяців тому

      But isn’t that literally the purpose of enums?…. To only expect and use those values

  • @patrickmullot73
    @patrickmullot73 Рік тому +4

    The whole purpose of ENUMS is to prevent passing arbitrary values but only one of the Enum types!

  • @BuyMyBeard
    @BuyMyBeard Рік тому +1

    Honestly, i just make my own string litteral type instead, it works better. I do it like this:
    type Direction = “Up” | “Down” | “Left” | “Right”;

    • @yxngboypolo
      @yxngboypolo 7 місяців тому

      Or just use enum type literals….

  • @bobbyraduloff
    @bobbyraduloff Рік тому +2

    Bro is complaining that enums work like they’re supposed to. Not being able to pass in the raw value is the entire purpose.

  • @ciniss
    @ciniss Рік тому +1

    that's actually counter-intuitive, thanks for explaining

  • @EvgenyBobkin
    @EvgenyBobkin Рік тому +3

    How about `type Foo = "one"|"two"|"three"`. I like enums, to me it looks like creating problems where they do not exist😂.

    • @Computeshorts
      @Computeshorts  Рік тому

      That stops you from using Levels.DEBUG the power of this is that you can use the string and the type interchangeably rather than being limited to one or the other

  • @fabricehategekimana5350
    @fabricehategekimana5350 Рік тому

    Rust enums are the best. Typescript enums should learn from that (sum types supremacy)

  • @yanisfourel
    @yanisfourel Рік тому +1

    title should say "Typescript"

  • @iwolfman37
    @iwolfman37 Рік тому +1

    lol enums don't even exist in golang

  • @adriankal
    @adriankal Рік тому +1

    First mistake: use typescript to do pretty much anything.

    • @yxngboypolo
      @yxngboypolo 7 місяців тому

      I don’t like TS = bad developer

  • @romanstingler435
    @romanstingler435 Рік тому +1

    Use RUST and LOVE youe ENUMS

    • @Computeshorts
      @Computeshorts  Рік тому +1

      There is so much hype around Rust I never managed to check it out but I plan to this year. Will probably change my mind on enums then 🤣

    • @romanstingler435
      @romanstingler435 Рік тому

      @@Computeshorts ua-cam.com/video/Epwlk4B90vk/v-deo.html

  • @popplestones886
    @popplestones886 Рік тому +1

    Title should be stop using typescript