Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'unknown' vs. 'any'

TypeScript 3.0 introduces unknown type, according to their wiki:

unknown is now a reserved type name, as it is now a built-in type. Depending on your intended use of unknown, you may want to remove the declaration entirely (favoring the newly introduced unknown type), or rename it to something else.

What is difference between unknown and any? When should we use unknown over any?

like image 994
Jac Mos Avatar asked Jul 20 '18 09:07

Jac Mos


People also ask

Whats the difference between unknown and any?

unknown is the type-safe counterpart of any . Anything is assignable to unknown , but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.

Why unknown is better than any?

Conclusion unknown and any are 2 special types that can hold any value. unknown is recommended over any because it provides safer typing — you have to use type assertion or narrow to a specific type if you want to perform operations on unknown .

Where do I use unknown TypeScript?

The unknown type is commonly used to avoid the any type. Instead of having no type or any type, we assign it to unknown . Everything assigned to this type will result in an error unless you assign it to another type at some point.

What is an unknown type and when to use it in TypeScript?

In Typescript, any value can be assigned to unknown, but without a type assertion, unknown can't be assigned to anything but itself and any. Similarly, no operations on an unknown are allowed without first asserting or restricting it down to a more precise type.


1 Answers

You can read more about unknown in the PR or the RC announcement, but the gist of it is:

[..] unknown which is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.

A few examples:

let vAny: any = 10;          // We can assign anything to any let vUnknown: unknown =  10; // We can assign anything to unknown just like any    let s1: string = vAny;     // Any is assignable to anything  let s2: string = vUnknown; // Invalid; we can't assign vUnknown to any other type (without an explicit assertion)  vAny.method();     // Ok; anything goes with any vUnknown.method(); // Not ok; we don't know anything about this variable 

The suggested usage is:

There are often times where we want to describe the least-capable type in TypeScript. This is useful for APIs that want to signal “this can be any value, so you must perform some type of checking before you use it”. This forces users to safely introspect returned values.

like image 97
Titian Cernicova-Dragomir Avatar answered Oct 13 '22 19:10

Titian Cernicova-Dragomir