I am still rather new to TypeScript and trying to work on my knowledge and intuition about when to use which types.
When would you use unknown
vs. object
?
From https://www.typescriptlang.org/docs/handbook/basic-types.html:
About object
:
object
is a type that represents the non-primitive type, i.e. anything that is notnumber
,string
,boolean
,symbol
,null
, orundefined
.
About unknown
:
We may need to describe the type of variables that we do not know when we are writing an application. These values may come from dynamic content – e.g. from the user – or we may want to intentionally accept all values in our API. In these cases, we want to provide a type that tells the compiler and future readers that this variable could be anything, so we give it the
unknown
type.
Is unknown
a strict superset of object
?
Is unknown
maybe precisely object
+ number
+ string
+ boolean
+ symbol
+ null
+ undefined
? If not: what's missing -- precisely, or conceptually?
If the TypeScript version matters for answering this: let's assume 3.9 :-).