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

After introduces the any type, which bypasses type checking, Mike reviews the never type. The never type is the return type for functions that never return and never is the type of variables under type guards that are never true.

Get Unlimited Access Now

Transcript from the "any & never Types" Lesson

>> Mike North: So let me add one more interesting thing here before we jump in to our next exercise. There's something called the any type. And think of this as a wild card type, right. Anything could be assigned to a variable that is of the any type. So in this example we say age = 34.

[00:00:22] So age's type is a what?
>> Speaker 2: Number.
>> Mike North: Number. And then we have myAge, and we say age as any. So we're casting this to the any type. myAge's type is now any. And so now we're able to assign myAge. We can give it values of whatever we want.

[00:00:44] Booleans, strings, anything like that. Anything is assignable to an any. It is easy to remember there. The way you can think of this is, you're back to regular JavaScript. This is the way a non typescript world treats things like any variable that's defined with let. You can reassign it whatever you want.

[00:01:10] And I talked about how you could have typescript to kind of chew on regular JavaScript code and make sense of it, and give you some information. This is a big part of how it's able to do that. Where it can't infer the type of a variable. It'll just give it the any type and it'll allow things to play out from there.

[00:01:32] And as you tighten things up, you can sort of make that constraint more accurate and more tightened up to what you actually expect the range of those values to be. Typically what you do is, say we have a JavaScript piece of code and we're converting it to TypeScript.

[00:01:52] You would say anything goes, we're just gonna change this JS file to TS. You would end up with a lot of implicit any's. Right, where it's gonna go through your code and it's gonna say, all right, this is a function that takes two arguments, and it's a and b.

[00:02:10] And you return a + b, those could be strings, those could be numbers, I don't know. I can't figure it out, I don't have enough information in order to make a call here. See those would be implicit any's. So then, you would at some point, forbid implicit any's.

[00:02:28] And you'd go through and say, okay, well where we really wanna be flexible, we will have explicit any's, or we'll give it a type, a proper type. And then, you would try to purge explicit any's from your code base, and end up with things having a well defined type instead of sort of the catchall.

[00:02:50] The opposite of the any type is a never type. And nothing can be assigned to a never. So if we did like let. Let me take you to a code here. Our little scratch pad.
>> Mike North: So if we had something like this,
>> Mike North: That's fair game.
>> Mike North: This is not, nothing can be assignable to a never.

[00:03:35] You might ask, why would you do this? We'll dig into it a little bit tomorrow as we talk about types in a deeper way. But just think of it as something you would want in potentially unreachable sections of code. You can almost think of it like a throw except for the typescript compiler.

[00:03:59] Where if somehow you have a function that in some situation returns a never, now as the compiler is analyzing your code, it's gonna say, well okay based on the argument types that are passed in. I expect that you're gonna end up with a never here. And so this function is gonna return a never and you're trying to capture the result, and that's gonna be a never.

[00:04:20] That error will percolate up through. It is the compile time you think of it that way. So you wanna, it's for scenarios you're deliberately trying to avoid. In our case for the RGB staff, we might use a never in a situation where you're passing in like a string that is neither of size 3 or size 6.

[00:04:45] You might use a never type there. Basically now, we have a better shot of picking up on that problem at build time.