Unknown type in typescript

Rubin - Apr 25 '22 - - Dev Community

The unknown type in typescript is a common type that you will likely to come across. It is a strict version of the
any type and interms of accepting data, it accepts any data type which is similar to nature of any type. However
there is a minor differences between them. Since an unknown type also can have multiple type, it forces to use type assertion before using the types function. To further clear , lets look an example

 function readAny(val: any){
     return val.trim();
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we are using any type for the value. Since trim is a string function, this works well for string data. However on supplying numbers or booleans , we get a runtine error

The same example using unknown

 function readAny(val: unknown){
     return val.trim(); // typescript error at this line
}
Enter fullscreen mode Exit fullscreen mode

Typescript would throw a compile time error as we are using string function without type assertion. To fix it, the correct code would look like

 function readAny(val: unknown){

    if( typeof val === 'string')
     return val.trim();
}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .