- "primitive":
boolean
,number
,string
,symbol
,null
,undefined
- "object" aka "non-primitive": any "custom object" (e.g.
{ foo: 1 }
) or function
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types
(In JS, people mistakingly think of primitives as "object"s, but in strict JS terminology, they're not.)
An "object" aka "non-primitive" (as per JS terminology above).
const myObject: object = primitive // error
const myObject: object = customObjectOrFunction // no error
The class instance behind any "object".
primitive instanceof Object // false
customObjectOrFunction instanceof Object // true
// At first glance, we would expect this to error, so that TS' behaviour matches
// JS's runtime behaviour. However, TS only cares about structures, so this does
// not error.
const myObject: Object = primitive;
const myObject: Object = customObjectOrFunction;
// Note: `Object` value === `ObjectConstructor` type
An object
but specifically one without any properties (TODO: correct?).
const myObject: {} = {}; // no error
// At first glance, we would expect this to error, but it doesn't. TODO: why?
const myObject: {} = { foo: 1 };
@RyanCavanaugh That makes sense. I've also seen this referred to as the "empty object type", but that is surely misleading, because it's not only assignable to "object"s (under the traditional JS terminology defined above in this gist) but also some primitives (all except
null | undefined
). This naming is why my intuition lead me to believe{}
would be the same asobject
but specifically one without any properties.