6 TypeScript Typing System Tricks You Should Know

What I’ve learned from using TypeScript’s type system

Sunny Sun
Better Programming

Flowers
Photo by Melissa Askew on Unsplash.

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

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…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Responses (2)

What are your thoughts?

Some unpleasant properties of TypeScript not mentioned
Type Parameter Constraints for Generics
If you have a `type T<U extends V> = ...` and `V` is an explicit or implicit union type (boolean is an implicit union type as it behaves exactly as `true |…

--

Using explicits types seems to add code coverage. Btw less tests are needed to be writed. But maybe I'm wrong.

--