Sometimes, when you write code in a typed language, you have to work with values that are unknown to you at the time you write the code. When that happen, you can use
unknown and you can use type guards to maintain control of what types your code can handle.
- when expecting values from a third party library
- when expecting values from a user input
let value: any = 'hey!' value = true value = 1
The aforementioned code, won’t throw any error at compilation time. The reason is that the type
any skip the type checking.
Obviously, errors runtime errors will be triggers depending on the datatype and the assumption.
let value: any = true; value.toUpperCase() // Uncaught TypeError: value.toUpperCase is not a function
As we saw in the previous example, the
any type is really flexible, to the extent that it can be abused and can often cause unexpected errors.
The recently added
unknown type try to solve such problems. It can be assign to any value but you can’t access properties or call or construct the to the value marked as
An example of
unknown in action, along side the
let value: any = 'hello' value.toUpperCase() // 'HELLO value.map(e => console.log(e)); // value.map is not a function let secondValue: unknown = 'world' secondValue.toUpperCase() // Error: Object is of type unknown value.map(e => console.log(e)); // Error: Object is of type unknown
It’s not possible to interact, in any way, with a variable of type
unknown. It’ll always error at compiling time.
On the other hand, you can, always interact with a variable of type
any and it won’t error at compiling time. However, it might error at runtime.
unknown type would have been really useful in one of my previous companies. We were using Angular (framework based on TypeScript) but he had to move really fast and build several product from scratch in a very short period of time. We abused of the type
unknown would have been really useful for the developers that wanted to move fast, without breaking things. We didn’t have it at that time, but now we do and it’s awesome to break things!
If you have been, thank you for reading, keep on coding!