This course has been updated! We now recommend you take the TypeScript Fundamentals, v3 course.

Check out a free preview of the full TypeScript 3 Fundamentals, v2 course:
The "Type Aliases & extends" Lesson is part of the full, TypeScript 3 Fundamentals, v2 course featured in this preview video. Here's what you'd learn in this lesson:

After giving an overview of what will be covered in the section, Mike explains what type aliases do for the user.

Get Unlimited Access Now

Transcript from the "Type Aliases & extends" Lesson

>> Mike North: Earlier we had a question about the difference between interfaces and something called type aliases. And these are both ways of giving a structure a name that we can import and export from modules, and we can refer to. The main benefit is we have one central place where a type is defined, and it can be used throughout a code base.

[00:00:22] So we're gonna study these two concepts, and compare and contrast them. Along the way, we're gonna touch on different types of signatures which includes the kind of signature we're used to for invoking a function, and something called an index signature. We will discuss the idea of open interfaces, and this is a way that you can augment types that you may import from a library somewhere.

[00:00:48] We will discuss the concept of access modifier keywords which gives us control over who can see methods and instance data when it comes to classes. And finally we'll touch on heritage clauses which is just a fancy name for extends, right? You're used to extends from JavaScript classes. There's a new one called implements that comes with with TypeScript.

[00:01:13] So we'll open our notes to the page that starts with three, interface and type basics.
>> Mike North: Type aliases, a really simple concept. It is literally giving a type a name. Any type that you can use with a variable, you can also create a type alias for. So, when I say string or number here,

>> Mike North: What I mean here is anything that could exist in this space, you can create a type alias for. This is not true for interfaces. So type alias is actually more flexible than an interface in some way. This is the only time you'll ever see a type up here on the right hand side of an equals.

[00:02:11] Well, there is one other but, it is really the identical concept just in a slightly different syntax, but we're exclusively looking in type space here. This will compile to no JavaScript at all.
>> Mike North: One of the challenges with type aliases is that they are defined and figured out by the compiler in terms of what values are allowed, in line as the file is parsed.

[00:02:47] And this prevents you from creating self reverential types. So let's say we had a data structure that we wanted to create a type for, that looked like this.
>> Mike North: 1, 2, 3, 1, 1. Right, it consist of 1s, 2s and 3s, but also a nested array. Really simplified kinda JSONish thing.

[00:03:12] Like there's some hierarchy here in a set of allowed values. We could try to type this saying that the value can be 1, 2 or 3, or an array, where an array is an array of NumVals. Typescript compiler is not happy, and the reason is, it wants to conclusively figure out what NumVal is all about, before it moves on to the next line, and it has not yet encountered this.

[00:03:40] So, it runs into a circular problem. It's like a chicken and the egg problem. To define A we need B which needs A, and so follow that all the way down. So, we will look back to this and explain how this can be. We can use to our advantage.

[00:04:01] We can use this weakness as strength.
>> Mike North: So we've already looked at interfaces. I wanna point out that interfaces can extend from other interfaces, like same syntax that you're used to seeing with classes. Just remember that extends is used for inheritance of like things. Interfaces extend from interfaces, classes extend from classes.

[00:04:27] So here we're just adding country code to something that already has a phone number and a name.