TypeScript 2.2 introduced the object
, a type that represents any non-primitive type. It can be used to more accurately type methods such as Object.create
. Don't confuse it with the Object
type or {}
, the empty object type, though!
I’m having a hard time understanding primitive types. If null is one of the primitives, why is typeof null ‘object’?. And then why can’t we assign null to something with type object?
@Brendan: typeof null
evaluating to "object"
is a bug that we've been stuck with for two decades (see The history of “typeof null”); unfortunately, it can't be fixed without breaking a ton of JavaScript code.
When you use --strictNullChecks
(which you should!), null
is not assignable to object
. If it were, that would defeat the whole purpose of strict null checking.
Interesting