In TS they are not magic numbers, but rather type-level number literals, and the compiler won't allow you to put anything else in code field. So you can put your heart at ease, because this is a totally valid solution. If you know Haskell or Rust, you can see analogy with their type-level symbols.
I am Software Developer, currently interested in static type languages (TypeScript, Elm, ReScript) mostly in the frontend land, but working actively in Python also. I am available for mentoring.
Yes, exactly as Yuriy is saying. I hear very often this argument, that literal union is a magic string/number etc. It is not, as you create a type with few specific number members. There is no way to use different value, it is restricted by type. And also if this particular member will be removed from the allowed set, compiler will show all places where it needs to be changed. Also you need to import Enum every time it is used, for literals you don't need to do that.
I would have no fear for ADT made from literals. It is really ok.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In TS they are not magic numbers, but rather type-level number literals, and the compiler won't allow you to put anything else in
code
field. So you can put your heart at ease, because this is a totally valid solution. If you know Haskell or Rust, you can see analogy with their type-level symbols.Yes, exactly as Yuriy is saying. I hear very often this argument, that literal union is a magic string/number etc. It is not, as you create a type with few specific number members. There is no way to use different value, it is restricted by type. And also if this particular member will be removed from the allowed set, compiler will show all places where it needs to be changed. Also you need to import Enum every time it is used, for literals you don't need to do that.
I would have no fear for ADT made from literals. It is really ok.