4 ms·
I love using Typescript, but I only ever use interface and enum. What extra benefits would I get from using other features than those? What am I missing out on?
by globalise83 5y ago
I love using Typescript, but I only ever use interface and enum. What extra benefits would I get from using other features than those? What am I missing out on? :)
- rpastuszak 5y agoIMO 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.
- gherkinnn 5y agoTagged union types [0] are very powerful and let you model most things in very robust ways. type Response<Data, Error = string> = | { type: "Loading" } | { type: "Success", data: Data } | { type: "Error", message: Error } You won't have access to data unless type === "Success". You can model other mutually exclusive situations in the same way. Then, Typescript comes with very handy utility types. [1] And if you're having an especially good day, you can loose yourself in type level programming. Mapped types, for example. [2] Though this is only really necessary at a library level. Back in reality, type narrowing [3] and function overloading [4] are good to know about. And there's a lot more. - 0 https://mariusschulz.com/blog/tagged-union-types-in-typescript https://mariusschulz.com/blog/tagged-union-types-in-typescri... - 1 https://www.typescriptlang.org/docs/handbook/utility-types.html https://www.typescriptlang.org/docs/handbook/utility-types.h... - 2 https://www.typescriptlang.org/docs/handbook/2/mapped-types.html https://www.typescriptlang.org/docs/handbook/2/mapped-types.... - 3 https://www.typescriptlang.org/docs/handbook/2/narrowing.html https://www.typescriptlang.org/docs/handbook/2/narrowing.htm... - 4 https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads https://www.typescriptlang.org/docs/handbook/2/functions.htm...