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: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.