Check out a free preview of the full TypeScript 3 Fundamentals, v2 course:
The "Type Guards" 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:

Mike introduces user-defined type guards.

Get Unlimited Access Now

Transcript from the "Type Guards" Lesson

[00:00:00]
>> Mike North: We can even define our own type guards.
>> Mike North: So this is called, this is referred to as a user defined type guard. And if we look at the implementation here, what type does this return? What's this type here? Type of something equals string.
>> Mike North: It's a Boolean, right?

[00:00:26] Type of, if we were to capture that value, it's a Boolean. And a Boolean ended with another Boolean, it's gonna give us a Boolean. So the return here, we're definitely returning a Boolean. But we have indicated to the typescript compiler that it should regard the true or false value of this Boolean as an indication of whether x is of type has e-mail.

[00:00:55] And that allows us down here to use it just like we're using type of and instance of in the previous examples. We can use it to narrow from an unknown down to a has e-mail. These are super, super handy, right? This is where your own custom types that you have in your app, you can create nice guards that will allow you to handle specific cases.

[00:01:20] This is my favorite guard, I use it all over the place. Is defined. And we're bringing back generics here. Here's why. We have a tight parameter T. We take an argument, which could be T or undefined, and we return whether the arg is T. So we're trying to remove the possibility that it is undefined.

[00:01:48] And if we look at the return here, that is gonna give us the correct Boolean value if we're trying to figure out that this is a non-undefined value. It's value is defined. The reason I do things this way is because if I were to pass this a value that could be a string but could also be undefined, when the type checking matches that up, it's kind of like it does this.

[00:02:24]
>> Mike North: It almost lines things up this way. And so, T becomes string. So this is how I'm able to kind of pluck out the undefined. The argument I pass in
>> Mike North: Has it's undefined value removed. And this is useful if you have a list of things, like this.

[00:02:55]
>> Mike North: Right, so this is an array of type string or undefined. List.
>> Mike North: Filter.
>> Mike North: Filtered. We've removed all of the undefined values, and we've removed undefined from the type as well. So it's great if you have a collection, and say you have some optional value on a type, and you have an array of them.

[00:03:23] You can just attempt to pluck all them off, filter out all the undefined values. Things just work. I wonder if they fixed this.
>> Mike North: So this, actually, I think this is doing exactly what it should be. But the fact that string can have a falsey value means that an empty string passes through the Boolean constructor as false.

[00:03:46] Which is why we can't really tell the difference between an empty string, and a Boolean. They both will give the same truthy-falsey check. But it's really, really useful, is defined, when used in conjunction with filtering functions.