'unknown' vs. 'any'
The difference between unknown and any is described as:
Much like
any
, any value is assignable tounknown
; however, unlikeany
, you cannot access any properties on values with the typeunknown
, nor can you call/construct them. Furthermore, values of typeunknown
can only be assigned tounknown
orany
.
To answer your question of when should you use unknown
over any
:
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.
Take a look at the TypeScript 3.0 announcement for examples of type checking a variable of type unknown
and a more detailed explanation.
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.