Any and Unknown
The types any
and unknown
are used when we don't know the type of a value (e.g. dynamic content like parsed json).
Any
The type any
essentially disables type-checking for a value. A variable with type any
can be assigned any value, passed to any function, and called with any arguments, etc.
Unknown
The type unknown
is the type-safe version of any
. A variable with type unknown
can't be used for anything without first refining it to another type.
Many standard library functions use any
, but we should generally use unknown
instead. Unknown is relatively new - otherwise more standard library functions would probably use it.
Checking unknown keys
Due to current limitations of the in
operator, it's difficult to check if a key exists on an unknown
value.
This is one case where any
may make more sense for convenience. However, you can define a type guard function to accomplish this.
We'll cover type guards in more detail right after this