Let’s take a look at the
unknown type in TypeScript. Type
unknown is a very interesting type that only exist in TypeScript. Let’s take a look at how it works.
Working With Unknown
I always try to use the
unknown type when I need to use
unknown is like a type-safe version of
Let’s see how it is type-safe by looking at some examples.
This code will work even though the actual type of
a is a string because we defined the type to be
any. By doing so, TypeScript assumed that we know what we are doing and leave everything in our hands. Which is not good, because as you can see, we just assigned a string to a number. You can see how dangerous the
any type is.
Let’s see how we can make the same code much better using the
This time, the code won’t compile. We’ll get a complain from TypeScript instead.
This highlight the differences between the
any type and the
unknown type. We need to turn the
unknown type into a definite type before we can assign it.
b, TypeScript needs to know for sure that
a is of type
number. The easiest method to achieve that is by casting the
a variable into a
Or by using type assertion.
By explicitly casting the
unknown type like this, the TS compiler will allow us to assign the value. But as you can see, that’s still pretty dangerous. The compiler assumed that we know what we are doing, and leave everything to us just like
The advantage compared with
any is that we need to do explicit casting ourself. This eliminates the chance of accidental assignment.
For example: since I know that
a is not a number, I should turn
a into a number before we assigning it to
b to prevent bugs.
An even safer and more recommended way of dealing with
unknown is by narrowing its type using type guard just like this.
By using a type guard, the TypeScript compiler will know for sure that the variable is of a certain type within the guard block.
That’s it for the
unknown type in TypeScript.
We should always try to use the
unknown type instead of
unknown type forces us to double-check before using the variable. We are letting the compiler know what type the value of a variable should be when we need to use it. That’s why
unknown is considered a type-safe version of