Creating type aliases
In this lesson, we'll learn what a type alias is and when they are useful.
We'll cover the following
Understanding the need for type aliases #
In the last lesson, we created an object type to hold a name and a score:
const tomScore: { name: string; score: number; } = { name: "Tom", score: 70 };
const bobScore: { name: string; score: number; } = { name: "Bob", score: 80 };
const janeScore: { name: string; score: number; } = { name: "Jane", score: 90 };
Wouldn’t the code be more readable if we could somehow name { name: string; score: number; }
and use that name:
const tomScore: Score = { name: "Tom", score: 70 };
const bobScore: Score = { name: "Bob", score: 80 };
const janeScore: Score = { name: "Jane", score: 90 };
Well, this is exactly what a type alias is!
Get hands-on with 1400+ tech skills courses.