Avoid errors using unknown type

InstructorKamran Ahmed

Share this video with your friends

Send Tweet

unknown is the type-safe counterpart of the any type. The main difference between unknown and any is that with any you can do anything with the variable with the type set as any; while the variable with the type unknown only allows you to assign it the values and perform equality checks i.e. ===, ==, !== and !=, for any other operation, you must either create a type guard or specify the type using val as SomeType.

You should prefer to use unknown over any whenever possible because it gives you the same flexibility of any when assigning values to the variables but you must specify the type while using the variable.