6 TypeScript Typing System Tricks You Should Know
What I’ve learned from using TypeScript’s type system
TypeScript is about types. It’s a strongly typed language with powerful compile-time type-checking. At the same time, its static typing is optional, so it’s compatible with JavaScript.
I’ve been working with TypeScript for more than three years. Having learned a lot during this journey, I still have the surprise of learning new features or tricks in my daily work.
Here are some of the best practices I’ve learned by using the typing system:
- Be Specific
- Enable Strict Mode
- Apply Type Parameter Constraint to Generics
- Use Type Inference Instead of Explicitly Declaration
- Use any As Little as Possible
- Immutability With readonly
Be Specific
In TypeScript, types are shapes of data or structures of data (structural typing). With typing info, TypeScript’s compiler will give you early warnings to detect errors at compile time instead of letting you get an ugly runtime error.
When defining a type, we want to make the data structure as specific as possible. Using concise types will help the compiler…