Typescript is awesome. It offers so many great features. Here’s a summary of some of the greatest advanced Typescript features.
- Union and intersection types
- Conditional types
- Utility types
- Infer type
- Mapped types
By the end of this blog post, you should have a base understanding of each of those operators and you should be able to use them in your projects.
Union and intersection types
|| or the logical AND
&& to create new powerful checks.
We type the
orderProduct method with a union type. TypeScript will throw an error once we call the
orderProduct method with anything that is not a
number or a
An intersection type, on the other hand, combines multiple types into one. This new type has all the features of the combined types.
transferPlayer method accepts a type that contains all features of both
FootballPlayer. Only an object containing the
firstname and the
club property is valid.
Now that we know the union type. Let’s have a look at the
keyof operator. The
keyof operator takes the keys of an interface or an object and produces a union type.