Represent Non-Primitive Types with TypeScript’s object Type

Share this video with your friends

Send Tweet

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!

Brendan Whiting
Brendan Whiting
~ 7 years ago

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?

Marius Schulz
Marius Schulz(instructor)
~ 7 years ago

@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.

Brendan Whiting
Brendan Whiting
~ 7 years ago

Interesting