Skip to main content

about 4 years ago Syntax Podcast

TypeScript Fundamentals × Narrowing, Discriminating Unions, and Type Guards

Wes Bos

Wes Bos Host

Scott Tolinski

Scott Tolinski Host

Topic 0 06:35

Using type guards

Wes Bos

And if you have a function that takes in either a sandwich or pizza,

Scott Tolinski

then as you go, you might want to be using

Scott Tolinski

When things are defined as types, it's not always clear cut whether this thing is a string, a number, or whatever. Sometimes it's multiple things.

Scott Tolinski

and, it. Using many of these,

Wes Bos

before

Wes Bos

a sandwich, or just like how I wrote this function. You know? Is TypeScript wrong? No. You are wrong.

Scott Tolinski

how is this possible? I feel like I've done everything in my power to say that this is a string,

Wes Bos

written any code that makes you check that it is a toastable and then you have

Wes Bos

moving in. Right. So type narrowing would be just as part of

Scott Tolinski

It. It's it's amazing how, it. In so JSX or TSX or Svelte template code, all of that can be happy with the methods you're trying to use or pass down as things get more narrow. Yeah.

Scott Tolinski

it. A variable that was a number or a string.

Scott Tolinski

It it's like what? It's probably the most common error that you see in TypeScript script when you you're getting going with it. You're working it. You're you're feeling very comfortable TypeScript, and then all of a sudden you say,

Wes Bos

But it's also possible that somebody is passing in a pizza in this case, and you haven't food Sort of the area that you hit type narrowing and creating type guards on set. Like, I don't think we've actually ever talked about this on,

Scott Tolinski

Type narrowing is basically where you're saying, alright. I want to use a string method on this thing that doesn't exist in a number method or a method on a number type. So, therefore, I need to narrow the type down to be a string, so to say, so that we're not gonna get an error saying you cannot use 2 lowercase on property of number or whatever that ends up being.

Scott Tolinski

And Yeah.

Scott Tolinski

specific properties on one thing that don't exist on another thing. So let's say you had

Scott Tolinski

It. The most common error that you do see associated with this is argument type of this or this or this or this is not assignable to parameter type of this. It. Type blank is not assignable to type blank. Yeah.

Wes Bos

as part of that function, you would either check that it is a toastable food first before you go ahead and do the functions that are toasting Especially if it's, like, being extended. Yeah. See, b and o o p. It's not something that I do a whole lot of. I bet where that is handy is if you have a base type like food

Topic 1 09:13

Food metaphors

Scott Tolinski

So first off is going to be one of the easiest ways to type guard, something that you're going to see very commonly, and that's just checking the type of the thing in a conditional.

Scott Tolinski

it. We could have, like, a t minus or food was last mentioned.

Scott Tolinski

like, how many times the food

Scott Tolinski

examples creep into the show. It's funny it's so funny that it would be like, a string. Right? If maybe you have, like, a, a union type that's either a string or a number. If you successfully wrap in an if statement, if this variable is type of string, then do this following thing, then that's going to fix that error because now you've added a an explicit check to make sure that the thing is a string before you try to use the string method. And I use this myself even in template code all over the place,

Scott Tolinski

is equal to never the case. It's always something that you have done, And it's always a an issue with the the types that you're narrowing or attempting to narrow. So if you are getting some of these errors and you can't figure it out, And this is my, honestly, my first time seeing this as a strategy. I've just never had to reach for this, but I think this is really neat. I was looking into it, and I was like, why not just put a if statement inside the

Scott Tolinski

Sandwiches were last mentioned x amount of days ago on the show because it always ends up going there. But I think they're examples. Definitely.

Scott Tolinski

You'll frequently see this as, like, type of variable

Topic 2 13:44

Custom type guard functions

Wes Bos

So you could say function is a sandwich That's the only possible other type that's being passed in that would have an m p three property.

Wes Bos

that accepts a type of food.

Wes Bos

because that is not what your function does.

Wes Bos

Yes. You can take the logic out

Wes Bos

If any of the the type of the in the proper property check or the instance of, easy to use APIs and SDKs and, They offer a full sounds scarier than it is. What do you think about that? I was about to say that.

Wes Bos

But I guess Or you would convert like Scott said, you would convert the number to a string so that you're dealing with apples and apples before you go ahead and go through the rest of the function. Yeah.

Wes Bos

type guard.

Wes Bos

and that will return a boolean.

Wes Bos

And then from that you can just return unions, type guards, In JavaScript, so new date is an example. Or if you create your own you know, like, am I doing this wrong? Am I Their dev portal You might just go ahead and say toast the the bread, and it's gonna say, hey. Hold on. I see that you could toast

Wes Bos

Again, remember, a type guard is just a function that returns whether something is a type or not using all the methods we just talked about.

Wes Bos

inside your function? Right? That's what I usually do, and that's good enough.

Wes Bos

And that

Wes Bos

am specifically making this a type guard by saying in my case, the food argument that is passed to the function is sandwich,

Wes Bos

But the return type is not a boolean. It's a return.

Topic 3 16:30

this is type narrowing

Scott Tolinski

So you can specify this is type in the return position for methods and for methods in classes and interfaces.

Scott Tolinski

When mixed with type narrowing,

Wes Bos

Yeah. Tweet us at syntax of m if you know because I again, I'm not a big object oriented programmer, so it's definitely, As an instance of the one they created. I'm sorry. Can't speak.

Scott Tolinski

not one of those areas I've touched Yeah. It's one of those things that I see in the docs here, and I say that's

Scott Tolinski

tight. It's so funny to say. Okay. Yeah. It goes inside a constructor.

Scott Tolinski

Correct.

Wes Bos

Okay. Okay. That makes sense because it's if it's if it's being bound to that. Interesting.

Scott Tolinski

This is came out of left field. It it did. Might he have a left field. What didn't come out of left field, Wes, is our 2 sponsors today, which is Sentry and Payments Hub. So Sentry is the perfect place to see all of your errors and exceptions and mark them as done. Attach them to releases. Attach some to GitHub issues

Wes Bos

and you you run super and then super

Scott Tolinski

the type of the tag target object would be narrowed to the specified type. So you're basically saying Inside of the constructor.

Scott Tolinski

neat.

Wes Bos

will run the it. Original constructor first before you go ahead and and finish your your constructor of the extended type. I wonder if it's it's handy in those use cases.

Wes Bos

CSC Unions. So often a union is basically a list of types where it could be this or it could be this or it could be this. And often

Scott Tolinski

And then I never run into that issue in my code. So maybe there'll be someday when I'm Actually writing object oriented programming where the perfect opportunity to use this is will come up, and I'll a variable.

Scott Tolinski

I'll rejoice. But, Yeah. So far, I have not used it myself personally. I would love to see some real practical examples of it. Next one is this is kind of in the same area of type

Topic 4 11:29

in and instanceof type guards

Wes Bos

And the last 1 we have here is just a custom type guards, and that is Not anything that we didn't just talk about,

Scott Tolinski

With any types of classes or complex objects. Yeah. More in object oriented

Scott Tolinski

a date, then you would know that you have access to the the date

Wes Bos

one

Wes Bos

Instead of stuffing your functions full of if statements,

Scott Tolinski

instance of will also check up c s. The prototype chain.

Wes Bos

to narrow the branch, so to say. Oh, that's that's a really good example. Actually, I was just like I was Writing examples of like custom

Scott Tolinski

of as well for this type of thing where,

Wes Bos

classes, but like like an array, it will tell you that it's an object, right? Like almost everything in JavaScript it. Pass your basic, what, strings, numbers, booleans, am I approaching this wrong? Unfortunately, a string, a number of boolean, a custom type interface. But in this case, you say,

Wes Bos

but there is an idea in TypeScript.

Scott Tolinski

Yeah. Definitely, TypeScript Boss.

Wes Bos

the date is one that's built in, right? Anything that you need to use new on

Scott Tolinski

instance

Scott Tolinski

Variable is an instance of TypeScript is controlling my life too. I'm controlling TypeScript.

Scott Tolinski

So if you were to say let's say you had,

Wes Bos

is is much more, what's that from? Is that Simpsons? a property that you're looking for so you can use the property in object. Like, let's say you're trying to via the API. They do all kinds of stuff. And, like, This seems like But the only like the kind of like the weird thing or the interesting thing about that in TypeScript is the return type of a type guard.

Wes Bos

object oriented programing and which ones aren't and like which one you have to specifically choose If you need to be following different, That doesn't work with TypeScript types. So you couldn't say those fields that are different from each other. Right? Like you say, okay. The example, The TypeScript docs is really good as well as network state. Your network can either be loading, it could be failed, or it could be a success.

Topic 5 18:06

Discriminated unions

Wes Bos

or an error will be will be different. Right? And being able to check for those values Oh, that's a good one. Simpsons, I don't Do very many Simpsons jokes, but that one came out

Wes Bos

like a best before date is is the example that I've used. Right? You were so good with these food metaphors.

Wes Bos

Right.

Scott Tolinski

Otherwise, TypeScript is gonna yell at you again. Yeah. TypeScript frequently likes to yell at you, and that is one of the things that, like, Kind of turns people off of TypeScript

Wes Bos

Yeah, I found and I think I still find this, but especially When I started TypeScript, type narrowing. Oh, that Sounds complicated, Tied to using the dot notation is that if you're using classes, But if you pass in 2 things that were totally separate, then I'm going to catch you on that. Right? So you could do prop in. You can also just say, if object dot property. So if media dot m p 3, on those Can you narrow are just objects. So once you pass Those those sort of simple ones. There's a couple other ways you could check if something is a type. You could check if it has It might be. It's always cool. Yeah. I think that's it for me. Anything else to add here? No, I don't think so. Yeah. I think this is this nails it. Cool. Cool. Alright. Thanks for tuning in. Catch you on Wednesday.

Wes Bos

states and the network state will share and that might not be what you want. So if you wanna actually check if the instances on the thing or not, you can use the in. Yeah. And you can also use stuff with your payment integration, like the way that I would like to put it. And TypeScript will yell at you if you try to Assume that the data or the type coming in is what you assume it is, Something that is extending it that's not always the case. And we're gonna talk a little bit about some strategies here. Yeah. Another example I have here that's coming from my upcoming course is that you've got a function that takes in a food, And that food could either be a type like they like a food is kind of the generic type. And then we extend the generic food type and we create a sandwich and we create a pizza. Right.

Wes Bos

is important.

Wes Bos

within those, Let's say you have 3 types, Maybe do I need to step back 1 step I

Scott Tolinski

techniques that we talked about in this episode is, like, one of the key ways that you go from being like, where you're basically defining the type of this.

Wes Bos

Sandwich or pizza. They both I have their own properties. Like a pizza would have toppings

Wes Bos

But then they will not share things like a successful response

Wes Bos

Inside of your function

Scott Tolinski

occasionally, right, because it's yelling at you so much. But I do think being able to control types from type narrowing

Wes Bos

3 different if it is a type of of those functions and just put them in their own functions.

Wes Bos

passing in a value of food, and you have to check Peace. Peace.

Wes Bos

but they Also, we'll share things like,

Topic 6 04:00

PaymentsHub sponsor

Scott Tolinski

our listeners to yeah. Just to say, here's here's what's up. You know, here's here's what's going on. And, I think, payments have did a great job here. So, yeah, thank you for payments up for being a new sponsor. Yeah. Welcome. Alright. So let's get into

Wes Bos

on the podcast before. So you could still hit that and react as well, so You definitely need that in your tool belt.

Wes Bos

They have a really neat landing page that allows you just to quickly go through. They also have like a really they have like lots of products. And on the products page, it will tell you which ones are PCI compliant The idea behind it I saw was that some people were saying you shouldn't be putting that logic in your function a sandwich would have bread type, as an instance of,

Wes Bos

gives you easy to use APIs and SDKs that allow developers to create Ecommerce websites develop in person point of sale applications They also have they've sent us over some stuff that I'm just really excited about having this sponsor on myself because this seems really, really neat. Their code samples and everything looks pretty slick.

Wes Bos

TypeScript fundamentals. We've done a couple TypeScript fundamentals episode, it. And I was just going through all of the notes and and planning for my TypeScript course, and I was going through,

Scott Tolinski

Yeah. And and give that landing page a check too, like you just mentioned, because

Topic 7 19:55

Controlling vs being controlled by TS

Scott Tolinski

They did not properly account for that being undefined. Yeah. And and it should go to to say, you know, especially if you're new to TypeScript here. There are many times when it can feel like

Wes Bos

Or, complex It's not a good idea to replicate that check 3 or 4 times. In my case, it's almost always a one liner. Who cares? Right. But, it's nice to stick it in a separate function, I guess. It seems like it's the classic in TypeScript. You got to check that you got the right type before you go ahead and use it. It. There is no assuming in TypeScript, I think, is the, You have merchants in there. You have to process their payments. They provide interested in programmatic merchant onboarding and access We're going to dive into explaining.

Wes Bos

like it's making you. And it's not that TypeScript is is being,

Scott Tolinski

core problems, I think, when you get into TypeScript is learning how to something that you probably hit more

Wes Bos

Like, I I find it really helpful to talk to people who have have hit that possible issue. And the question is not how do I fix this is

Wes Bos

Google Home the other day and just said undefined. And I was like, that's a case.

Scott Tolinski

find and it. Just squish these types of issues in TypeScript because it is even though, like you said, it it is, you know, is yelling at you. It's at the end of the day, it is, you know, trying to save your butt. Or even it's looking now like,

Wes Bos

Well, it is it is annoying, but it's it's doing it because, there's a possibility that you could goof this up, or There's a possibility that somebody passes the very basic example that we didn't cover here is that it's possible that this thing is going to be undefined.

Wes Bos

And if you do not handle the case for this thing being undefined,

Wes Bos

annoying.

Wes Bos

behind that and and reshape how I wrote my types and extending prototypes, then that will also look up the prototype And the network state will have payment solutions. Our API rich product landscape is ideal Right? You don't wanna well, maybe you want would want to, but you don't wanna toast your pizza. You only wanna toast the bread of the sandwich. Right? So you might want to,

Wes Bos

I popped open something on my Google

Scott Tolinski

it. It's one of those

Scott Tolinski

Head on over to syntax s.fm for a full archive of all of our shows. And don't forget to subscribe in your podcast player or drop a review if you like this show.

Topic 8 02:20

Sentry sponsor

Wes Bos

data analytics

Wes Bos

a point of sale system. So, like, if you are building something will tell TypeScript this is a type guard and is good enough to check for what I want. Interesting that I've I've never

Scott Tolinski

Use the coupon code methods,

Wes Bos

That allows your your product to go ahead. Like you built a product and your product is used by people who then need to onboard other people. You need to underwrite them.

Wes Bos

treat all lowercase, all one word, and you'll get 2 months free. Thank you so much for Century for being such a great sponsor. We also we got a brand new Sponsor on. So hold on and listen to this. Payments Hub is a new sponsor of us, and they do some really cool stuff It's not me, is it? you are

Wes Bos

Around So if you a discriminating union is that,

Wes Bos

ad read here that I'll I'll rattle through for you as well.

Wes Bos

fleet of APIs that allow you to that you can integrate into your application. And this is not Just you throw it on your website and you sell something.

Wes Bos

I don't want to say the the Swiss army knife, but, like, this seems like the API set That you need if you need to be able to do some very often within or let's go back to my sandwich or pizza example. Right? Integrated into some software. You can integrate it into a chain,

Wes Bos

Some, accessing payment transaction data Through an API looks really sweet. So you want to check it out at developer.paymentshub.comforward/syntax.

Topic 9 01:01

Narrowing types in TypeScript

Scott Tolinski

or,

Scott Tolinski

I don't know. I've never heard that Can't collection of words in that melody. Canyon arrow. There's, like, a Oh, Canyon arrow. Yes. Oh. Yeah.

Scott Tolinski

even just users. You can create an issue directly from something that is an issue, an exception in your code. You can assign it to a user right then and there, and more importantly, you can see all of the things that you might want to know to solve that issue. For it. For instance, right, maybe what operating system? What browser? Oh, like, you can see that this is this is coming in from an Internet Explorer on an old Windows computer. You might say, you know what? It. I don't need to solve this issue. I don't I feel like I I don't need to solve this issue because this is gonna be more trouble than it's worth. Either way, Sentry is the perfect a place to keep track of everything that's going on in your application and make sure it's working its very best. So head on over to century.i0.

Scott Tolinski

Yeah. That kinda,

Topic 10 05:44

What is type narrowing?

Wes Bos

discriminating

Wes Bos

check it.

Wes Bos

and it kind of just boils down to,

Scott Tolinski

and TypeScript will make a pass out of you.

Wes Bos

I think this is one of those things that class and you want to say new possible state values, from very low level All the way through to to accept the payments. It looks awesome.

Wes Bos

So should we well, should we, like, start off with explaining what type narrowing is? Yeah. So type narrowing is basically,

Wes Bos

Exhaustively

Scott Tolinski

you you're thinking about

Wes Bos

So it's like, oh,

Topic 11 00:00

Transcript

Announcer

the craziest,

Scott Tolinski

Welcome to Syntax.

Announcer

Boss and Scott

Announcer

Monday. Monday. Monday.

Announcer

CSD.

Announcer

Open wide dev fans, get ready to stuff your face with JavaScript, CSS, node modules, barbecue tips, get workflows, breakdancing, soft skill, web development, the hastiest,

Announcer

the tastiest web development treats coming again hot. Here is Wes Barracuda

Topic 12 00:30

Talking TypeScript fundamentals

Scott Tolinski

fundamentals.

Scott Tolinski

it. Discriminating unions and type guards. Basically, It's always nice when these companies, they give us a little landing page just for, it kind of says Simpsons to me.

Scott Tolinski

Monday,

Scott Tolinski

hasty treat, it. We are going to be talking about some TypeScript On this ever done that before,

Scott Tolinski

we're gonna be helping you solve some of those errors that pop up in your TypeScript code That make you very frustrated because you say, hey. This thing is supposed to be this thing. Why is it giving me an error? My name is Scott Talinski. I'm a developer from Denver, Colorado, and with me, as always, is Wes

Scott Tolinski

We're gonna be talking about what is called type narrowing, that things that you would typically have on a date and not on a string. So in union types, you can always use instance of as well, is wrong. You're like, I've done everything I need to do here. TypeScript is wrong. And I'm gonna tell you straight up, that's

Topic 13 10:19

typeof type guard

Wes Bos

check if something was a podcast then at some point you're going to have a bad day and your application is going to crash or You're you're gonna send out an email that has undefined in it. Like, my, possible

Wes Bos

a dog or type of animal or any of the custom TypeScript types that you've created.

Wes Bos

or MP 3 in audio, then go ahead. And in that case, you're checking that it has, and TypeScript will say, okay, I'll let that go because So it's better to move it out into its own function, which I can see. And then also it's it's more reusable if you have a much more complex Something is is like the property is the type. So I've never there's nothing else in TypeScript like that. You're always returning food or new sandwich. Then then you can check if it is an instance of because that object has been generated different compliance, which is very much appreciated because I've gone down that rabbit hole before. Check it out. Developer.paymentshub.comforward/syntax.

Wes Bos

And you say, okay. I know that a podcast will have an m p three property. You could say if,

Wes Bos

m p 3 in podcast

Wes Bos

And the reason behind it actually doesn't doesn't even work all that well with objects. And the reason behind that is because if you say type of and in that function, there might be, like, toast.

Topic 14 15:53

Abstracting type checks

Scott Tolinski

abstract away Sweep under the the Yeah. Ugly stuff underneath the rug. Right? You have all these ugly if checks. You can just sweep it into a singular function that does the the check for you. It. I think that the syntax is the blank is food is sandwich as the return type is is really interesting as well.

Scott Tolinski

this is type

Scott Tolinski

Now I also saw a type narrowing with this is. Have you have you seen that, Wes? Oh, I don't think so. This is. Yeah. So, This is an interesting one where you can use

Share

Empowering developers for over 286197174628 milliseconds!