DEV Community

Discussion on: More accurate the type, better the code

Collapse
 
ybogomolov profile image
Yuriy Bogomolov

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.

Collapse
 
macsikora profile image
Pragmatic Maciej

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.