4 ms·
IMO you should ditch enums (just use union types 'value1' | 'value2' etc...) and stick to writing "statically typed javascript". You'll get enough value from it
by rpastuszak 5y ago
IMO you should ditch enums (just use union types 'value1' | 'value2' etc...) and stick to writing "statically typed javascript". You'll get enough value from it.
Incomplete and (very) biased list of why I find this approach useful:
- Amazing static analysis capabilities
- Being able to design the code by writing types first and just filling the gaps between them
- Less code which needs to be covered with unit tests
- Autocomplete for any 3rd party library (e.g. AWS, or try spiking some backend code with Contentful and navigating their data structures, it's sooo much easier once you describe your models using TS)
- Less thinking about "how" and more about "what" I want to build
- Still reads like JS so the barrier of entry for colleagues is easy
I'm not a big fan of interfaces, but I understand why they exist (declaration merging/patching mutable JS scopes/objects).
Edit: I just realised that I got overly excited and perhaps went too broad with the list of pros, but hope this is still useful. The official TS documentation says that you should default to interfaces instead of types, but the approach that worked really well for me is to start with types and use interfaces only when really necessary. Somehow this model fits better in my head, but that's maybe because my coding style is more functional than OOP.