1) If the object would only be used as enum, then use an enum or union. But in a real world scenario the object would probably be more complex, and used in code to do things with.
2) That's exactly what the ValueOf helper creates. The union type. Sure, you can write it manually, but then you'd need to keep it in sync with the object as well.
Basically, both questions boil down to "code first". Instead of defining all types and then writing code complient with those type, we write the code and infer types from it. And with that, typescript becomes much more flexible, while providing the same level of confidence.
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.
Thanks!
1) If the object would only be used as enum, then use an enum or union. But in a real world scenario the object would probably be more complex, and used in code to do things with.
2) That's exactly what the ValueOf helper creates. The union type. Sure, you can write it manually, but then you'd need to keep it in sync with the object as well.
Basically, both questions boil down to "code first". Instead of defining all types and then writing code complient with those type, we write the code and infer types from it. And with that, typescript becomes much more flexible, while providing the same level of confidence.