TypeScript Enums are Bad // Alternatives to use

Поделиться
HTML-код
  • Опубликовано: 12 сен 2024

Комментарии • 31

  • @TheFerroman
    @TheFerroman Год назад +4

    Great video and valid points.
    Nevertheless, I think you understand the DRY principle wrong. This is not about "try not to repeat words in code"
    It's actually a pros, that simple string can't be passed instead of enum.
    I'm a bit confused with "refactoring by typescript", isn't it done by IDE of choice?

  • @thebigfriezy
    @thebigfriezy Год назад +6

    Enums are not bad. If you use them incorrectly, then yes, you have bad enums. Enums can be super useful for codebase maintenance, especially when you are sharing typescript libraries across multiple repos. Third party library enums are particularly useful when you are expected to use their defined values, but are unsure of what is allowed. It can save you a trip of code diving to derive implementation. Definitely a misleading title.

  • @ahmadalghali90
    @ahmadalghali90 Год назад +3

    Extremely helpful, slick editing and straight to the point, thanks so much for sharing your knowledge!

  • @lsantosdev
    @lsantosdev Год назад +1

    I think numeric enums are now being checked in TS, if you try to use numeric enums with any number that's not defined in the enum itself, it doesn't work anymore, at least as of TS 5, I can't send the link here bc youtube blocks me so, try this same example on the playground you'll get: Argument of type '100' is not assignable to parameter of type 'loginmode'.

  • @anuragroy11
    @anuragroy11 Год назад +5

    Using only string enums from the very first day and I do not regret it at all. Easy to use and great for readability.

    • @cas818028
      @cas818028 Год назад +1

      Someone needs to tell all these TS pros to try actually coding professionally and review PRs all day.

    • @basarat
      @basarat  Год назад +3

      I don’t know about other pros, but I review PRs all day everyday. In unions I still trust 😂🌹

    • @Rac0p
      @Rac0p Год назад +1

      Yeah, confusing title, string enums are good, typeSafe from core, dont need "typeof" it is like a const but easy to use

  • @kmylodarkstar2253
    @kmylodarkstar2253 Год назад +1

    I think this could be a good approach if you build libs or something to share others. But for a company you could work with enums, they work well with the IDE. Nice video, thanks for sharing!

  • @sywareinformatique1745
    @sywareinformatique1745 6 месяцев назад

    Nice video! Just for information, you can convert literal enums to literal unions easily with a small trick:
    export function initiateLogin(mode: `${LoginMode}`) {
    // ...
    }

  • @siavoshoon
    @siavoshoon Год назад

    I cannot think of a better way to present this kind of content. Truly perfect. Thank you.

  • @antonioquintero-felizzola5334
    @antonioquintero-felizzola5334 Год назад

    Great video as usual Basarat! Thank you very much.

  • @leepowelldev
    @leepowelldev Год назад

    You don’t mention that at 7.20 this has the same non-dry issue as you mention twice for the enum examples. If you end up using that pattern there really isn’t much between it and a string enum.

    • @pastenml
      @pastenml Год назад

      There is though. Typescript enums are convenient initially but a real pain if you need to export them. String enums become incompatible with themselves if you for example import a library in another library. At least if you bundle, Then the library 1 types will not be valid for checking the second library types.

    • @leepowelldev
      @leepowelldev Год назад

      @@pastenml thanks, don’t suppose you have an example?

  • @tomldev
    @tomldev Год назад +1

    Been waiting for this one! Awesome video 💪
    Completely agree. Enums have always been flawed.
    What are your thoughts on const enums?

    • @basarat
      @basarat  Год назад +1

      const enums are great for high performance computing like game dev or compiler development.
      Only use them if your system doesn’t talk to anything else or it’s all locked in stone 😂
      Will cover them separately in the future 🌹

  • @tylerslater
    @tylerslater Год назад

    What theme are you using?

  • @seelvek6550
    @seelvek6550 20 часов назад

    Thanks! 🙇

  • @vincent4652
    @vincent4652 Год назад

    I feel like using a union of strings and passing in one of those string literals is too close to using "magic values". It makes code less readable at a glance. If another dev tries to work with your code in the future, they will have to go to every definition of every function to figure out which string union each one is actually using.

    • @ConanCoupe
      @ConanCoupe Год назад

      Nope! Your IDE will display the permitted strings at the call site of that function.

  • @supa1009
    @supa1009 Год назад

    Great video, thank you! : )

  • @Nevertheless013
    @Nevertheless013 Год назад

    Another great video sir. Unions are way better than enums

  • @pawekoaczynski4505
    @pawekoaczynski4505 Год назад

    6:40 I had no idea that is possible

  • @gutylabs9058
    @gutylabs9058 Год назад

    excelente... explosión del cerebro

  • @ThanHtutZaw3
    @ThanHtutZaw3 Год назад

    6:41 why "enter to rename " . It is IDE feature ?

    • @Aaron-qy7vb
      @Aaron-qy7vb Год назад +1

      on VSCode:
      alt + R (windows)
      Option + R (mac)

    • @ThanHtutZaw3
      @ThanHtutZaw3 Год назад

      @@Aaron-qy7vb so it is vs code replacing. I don't know that command. Thanks mate

  • @cas818028
    @cas818028 Год назад +5

    Bro you just got a unsub. This is total crap and I am so sick of people taking a crap on enums. I don't think you work in team environments or have to read PR's all day. I cant how many times people comments on PR's cause they thought people were passing "strings" around everywhere only to find out later it is a union type. And you only know that because the type is defined in some other file or package outside of the PR. Enums give a CLEAR and CONCISE, DEFINITE Intetion that the value is of a type/constant. All you TS fanboys need a reality check

    • @basarat
      @basarat  Год назад +5

      Further food for thought from your working professional friend ❤️
      If you have string unions AND string Enums in your code (most likely). How do you make the choice. How do you keep it consistent. Do your colleagues give mixed signals to each other in PR reviews (sometimes asking for Enums other times asking for unions)?
      I’m not against string enums. But they don’t have a strong enough reason to exist and be taught.
      Library definitions will be using string unions because they are easier so might as well make your code consistent.
      Anyways, I’ve known you long enough to still appreciate you 🌹
      PS: I’m not a TS fanboy. Otherwise I wouldn’t be spending ages teaching other things I know, even though they don’t do as well on the RUclips algorithm. I’m here to teach and make the world a better place, thinking sometimes about my own sustainability 😅

    • @pastenml
      @pastenml Год назад

      @cas818028 Don't be a hater. Just search for "typescript enum" here and you will see a lot of other typescript experts saying exactly the same thing as basarat, because they are right. Even the TypeScript maintainers often call them a mistake or recommend against them, because they are a foot gun. Keep using them until you shoot yourself in the foot though, and then you will find these videos again. If you're not building a library it's likely you can keep using enums and not get into any issues.