Types union and how to distinguish them?
doing so I can check later if kind === EventType.Click, and typescript will know that inside that block the object is ClickEvent.
Is that a good approach to this?
Also, could I use instead of
kind: 'click';
the actual type value? EventType.Click
?
What do you think of enums? Should I have created it as enums instaed of const object?
Thanks alot!
1 Reply
I think your code is completely fine.
If you’re interested in learning more for when/why to use enums… this is a fantastic post on optimizing JavaScript that includes a good section about string comparisons vs enums
https://romgrk.com/posts/optimizing-javascript
For your use case I can’t imaging it mattering without this event being checked tons of times, but still worth understanding the trade offs and when it matter I think.
Optimizing Javascript for fun and for profit
romgrk's personal blog