DEV Community

Discussion on: Typescript is actually amazing.

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

Yup, TypeScript's awesome!

My personal favorite trick is to restrict a certain argument to a fixed set of values using union types.

When used appropriately, this makes it far less likely to accidentally assign the wrong value to a variable (since type matching alone still leaves room for typos).

For example, if you denote a currency as a string, that's a bit too generic. Is any string, like Hello World, a valid currency? If instead you use union types and list out all the accepted currencies in your application, you'll get some really useful auto-complete suggestions as you code and won't have to worry about making typos in that respect.

(I don't recommend going overboard with this, though.)