TypeScript - Avoid Errors Using Unknown Type
Ivan Sevilla - April 22 2022
It's common to use any type in typescript when you don't know or you are not sure about the correct type to use. In this post I'll recommend you to use unkown type and explain why is a better approach.
Let's get started understanding that any type means that the value variable cand hold arbitrary values such as strings, numbers, objects, functions or whatever you want.
let value: any;
value = "hello world";
value = 1;
value = {};
value = [];
value = undefined;
value = null;
value = () => console.log("hello world");
Further we can hold anything, also we can do anything we want and here is the problem. Because we can use the value such as a text but actually we are manipulating a number. So we'll have a runtime error without previous advice.
let value: any;
value = "Hello";
value = 22
const upperCaseText = value.toUpperCase(); // π₯ TypeError: value.toUpperCase is not a function π₯
console.log(upperCaseText);
Using any type we don't have typescript protections, because we can hold arbitrary values and do anything we want. If we really don't know a type for a variable a safer and better approach would be using the unknown
type.
With unknown
type we can assign all types to unknown
just like we can assign all types to any
. The difference is that any
allow everything and unknown
allow almost nothing.
Here, value
is type unknown so we can't call to toUpperCase
method. We have to narrow the unknown type to something more specific, in this case the type string. We can do this narrowing using the typeof
operator.
let value: unknown;
value = "Hello world";
value = undefined;
if (typeof value == "string") {
const upperCaseText = value.toUpperCase();
console.log(upperCaseText);
}
So, we are ensuring that the value is an string before use it as a string. The error is go away because in the if statement the value is type unknown but inside is an string, so we can call to toUpperCase
method without having a runtime error.
Conclusion
If you don't know the type use unknown and avoid errors that can be avoided π.
- β’ Edit on GitHub
Subscribe to the newsletter
Subscribe to receive my posts by email.