Any vs Unknown In TypeScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you may think that the any type and the unknown type in typescript are the same thing or at least similar but in reality they're actually complete opposites of each other I have two variables here a which is of type any B which is type unknown and if I come in here and I just try to call something on a and I just try to call something on B it doesn't matter what it is you're going to notice a doesn't give me any errors and that's because it's type any which means it could literally be anything so they allow you to do anything at all that you want and no one on the other hand means that b we don't know what it is so we don't allow you to do anything at all if we hover over B you said B is of type unknown that's why we're getting an error because you can't call anything on it without checking what it is first so the real way to remember the difference between the two is any lets you do anything that you want while unknown restricts you from doing anything at all this is why when I have the choice I generally like to use unknown instead of any because any just essentially removes all type checking entirely while unknown forces you to be incredibly strict with your typing
Info
Channel: Web Dev Simplified
Views: 92,833
Rating: undefined out of 5
Keywords: webdevsimplified
Id: 5SjoMNVwd4s
Channel Id: undefined
Length: 0min 58sec (58 seconds)
Published: Thu Apr 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.