Transcript
00:00 Okay. So here we have this dangerous process function that accepts a value and it's set to any. So here's the funny thing about any. You literally can do anything with any and TypeScript is totally fine with it. It's effectively saying, like, I don't, I I don't wanna use TypeScript.
00:17 You're basically using JavaScript at this point. There's no compile time catches or anything like that. Whereas if you change it to unknown, then you're going to get a warning. And it's, you still don't know what it is and so it's it's not, it doesn't have a problem with the weird function that we're trying to call. Instead, it's just saying, hey, you haven't checked whether, what this thing is.
00:39 So I can't actually do anything with it. So this is why unknown is typically better to use than any because it makes you safer. It ensures that you've at least checked, hey, does that function exist on this value? And so here we're gonna say to upper case and, we still need to double check that, hey, does this value, or is this value a string? So here, this is one way that we could do that.
01:03 Let's say type of value. If if that's not equal to a string, then throw an error. And that works just fine. And the nice thing is that, yes, there would technically be an error at run time either way. But, in this case, we're, be able to capture that and throw a more helpful error.
01:22 Another way that we could do this, if this was an important thing for us to do, is to check a couple different ways and and, process it as a string based on what its type is. So if we could say, if the type equals a string, then, we'll return to upper case. Otherwise, if it's a number, then we're gonna set to fixed, two. And then if it's a boolean, we'll make it a string. And if it's anything else, then we'll just pass it to the string constructor and it will construct a string out of it somehow and hopefully it works out nicely.
01:57 And with that, we are ready to go. So, oh, I forgot. This is my bad. I wasn't even following my own instructions. I rewrote dangerous process rather than, making a new version of it or I I changed dangerous process.
02:11 But, anyway, hopefully, you get the idea, of what this is supposed to do, and we'll just put that there so we can see it. There it is. Hello. So, that is any versus unknown. You will find that, there are, built in types for, various package or or, APIs that are built into the browser, built into the, JavaScript that have any.
02:36 For example, if we do array dot is array and we dive into that, we're gonna see that this is using any. This is I suspect, this is most likely for backward compatibility reasons because any is a lot newer than unknown. And changing from any to unknown would make any consumers have to change a lot about the way that they're writing their code. And because there's so much TypeScript's, code in the world, it's really hard to make a change like this, without just breaking everybody and now they have to go and update everything and it can be a big pain. And so, you'll find that, any is actually used, quite a lot inside of libraries and stuff like that in large part because of backward compatibility issues.
03:21 But in your code, you should default to unknown and only use any when unknown is just not serving you very well. Alright. That is, just just peachy. Let's move on to the next.
