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 "Top Types" 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 the unknown and any types.

Get Unlimited Access Now

Transcript from the "Top Types" Lesson

>> Mike North: We've been working with TypeScript's top and bottom types which are any and never respectively, in a few different ways today. So let's dig a little bit deeper into this topic and give it some proper attention. We'll look at how we can pass private or unknown values through a piece of TypeScript code.

[00:00:17] We'll look at exhaustive conditionals. And, finally, we'll take a look at all of the various kinds of type guards we can build and use that can be used with conditionals to narrow a type down, so you can get something more specific. So let's open up our notes to number 6, guards-and-extreme-types.

>> Mike North: So, first, let's take a look at top types. TypeScript actually has two of these, although one is a relatively recent development. Any, of course, is a top type. But TypeScript also has a type called unknown. Like any, unknown can receive any value. You could assign it to a string or to a number, whatever you want.

[00:01:13] The different between these two is apparent when we look at how you can access things off of these two values. So for the any, you can see that we're able to grab, we're treating it like it's a nested object and there is no type checking error there.

[00:01:34] And this is you're playing by JavaScript rules, right? No type checking. Unknown, while it is very happy to hold any value, you cannot use unknown directly. You must narrow it in some way before you use it. A good example of where you might want to make use of this is an API response where you know you've got some JSON back.

[00:02:01] But you need to perform some sort of assertion to make sure it really is what you hope it is, before you continue down that path.
>> Mike North: And we'll look at how you narrow that down and it's through using Type Guards. And we'll see that you it can hold anything you want, but we gotta do something before we can access it.

>> Mike North: In terms of when you might want to use any, they're great for parts of our programs where we want to retain a lot of flexibility.
>> Mike North: So, in this example, we can see that this function, it just is interested in printing the resolved value of a promise out to a console, right?

[00:02:44] So all it does is, it waits for a promise, grabs the resolved value, and passes it to console.log. And if we look at the type of console.log, it turns out, oops, this is the interesting one. I guess they're both, they're somewhere here. They both take anys, right? That's what you're passing to console.log.

[00:03:04] You can pass it functions and classes, and it has it's own way of serializing things out. So, there is no reason for us to further limit what kind of promise we can handle in this way. Any is absolutely the appropriate type to use in this case.
>> Mike North: In terms of when we want to use unknowns, they are good for either values that are either private where maybe you have a library and you pass a value to somebody and they might pass it somewhere else but you don't want the to know the structure of this thing.

[00:03:40] Not for a security reason, but maybe you don't want them to depend on the structure of this thing. You don't want that to become part of your public API, right? So an example of this might be, whatever set timeout returns. Do you really care that that's a number?

[00:04:00] It's a number because you pass a number back to clear timeout, right? So you can say, set timeout, in five seconds invoke this function. And it's almost like it gives you a little ticket that you can use to cancel that later on. But the fact that it's a number, is not really important to you.

[00:04:16] And maybe if this were a part of a library, the author would want the freedom to be able to change this to some other thing and to not consider that a breaking change. So that would be a great use of unknown. But we can certainly see that, as is, they are not really usable.

[00:04:36] So what we need is a type guard in order to sufficiently narrow this type and to be able to use it. Here are two that you'll already be familiar with, because they have meaning in the JavaScript world, typeof. Inside here, myUnknown is of type string. So out here it's unknown, in here, it's a string.

[00:05:00] And then string.split works fine. Instanceof Promise, out here it's unknown. In here it's a Promise any. So this would be how you'd sort of get it to something useful.