Learn TypeScript
Introduction
Introduction
In this module, we are going to learn about conditional types. As the name suggests, these are types that depend on conditional logic. Like mapped types, conditional types transform a type into a new type.
Conditional types are useful for removing type within a type. Consider the following types for an age:
type Age = number | null | undefined;type NonNullableAge = number;
Our types would be more DRY if we could do the following:
type Age = number | null | undefined;type NonNullableAge = NonNullable<Age>;
Well, NonNullable
is a standard utility type in TypeScript that is implemented using a conditional type to remove null
and undefined
from the type passed into it.
We are going to learn how to implement our own conditional types in this module.
In this module we will cover:
- Creating a basic conditional type
- Inferring types in a conditional type
- Creating some standard conditional utility types
- Quiz