Any vs Unknown

any vs unknown in Typescript – which one should you pick

I was quite curious about the fact that Typescript supports both any and unknown types which can be used to store any type of data without strict type checking. I had no Idea why there are two types for the same reason. I recently came to know their differences. Lets understand with below snippets.

Figure 1: any type in Typescript does not restrict invalid operations
Figure 2: unknown type in Typescript restricts arbitrary operations to a variable, requiring us to use narrowing
Figure 3: Type narrowing in action for unknown types

Photo by Chien Nguyen Minh on Unsplash

Leave a Comment

Your email address will not be published. Required fields are marked *