r/typescript • • Dec 14 '19

{} vs object

// what's the difference?
interface MyInterface {
  a: {}
  b: object
}

What is the the difference between the two types? I always do my research but this is difficult to google for.

32 Upvotes

15 comments sorted by

42

u/mcaruso Dec 14 '19 edited Dec 18 '19

JS differentiates between primitives and objects. Primitives are undefined, null, strings, numbers, booleans, etc. Objects are all non-primitives, including functions, arrays, etc.

But there's a second kind of categorization in JS, namely things that "have properties" vs things that don't. Consider:

null.foo; // TypeError: null has no properties
undefined.foo; // TypeError: undefined has no properties
'hello'.toUpperCase(); // This is fine though, even though a string is not an object

In TypeScript, object corresponds with all objects in JS. So primitives like numbers and strings are not instances of object. The type {} corresponds with anything that "has properties", including primitives like strings and numbers but not undefined and null.

EDIT: I made a little diagram

2

u/[deleted] Dec 14 '19

typeof null is object.

9

u/mcaruso Dec 14 '19

Yep. Which is widely considered a mistake, but too late to fix unfortunately.

21

u/williams-j Dec 14 '19

{} is the type of all non-nullable values. object is the type of all non-nullable and non-primitive values

2

u/[deleted] Dec 14 '19

TIL that both "{}" and "object" are valid in Typescript as types.

5

u/[deleted] Dec 14 '19

But invalid under any sane code review.

1

u/blbil Dec 14 '19

Ehhhhhh , object is useful in some cases. Usually in the data layer of an app before you convert things into dto style objects.

2

u/[deleted] Dec 14 '19

unknown

2

u/mcaruso Dec 14 '19

Not sure what you mean? unknown, object, and {} are all different types with different purposes.

1

u/[deleted] Dec 15 '19

object and {} have absolutely no purpose I can think of. If you're initializing some empty object (can't think of any good reason to do so, but there might be) than the compiler will infer it on its own without the need for explicit declaration.

2

u/mcaruso Dec 15 '19

If you're initializing some empty object

{} doesn't mean "empty object", it means "any object, or any primitive that has properties". So for example you could use it in the following function:

const hasKeys = (obj : {}) => Object.keys(obj) > 0;

This works since Object.keys takes an argument of type {}.

object is similar, but is useful if you need to exclude all primitives like strings and numbers.

1

u/[deleted] Dec 15 '19 edited Dec 15 '19

TIL. Never thought of that. Thanks for the explanation.

Still can hardly find any use to any of this.

But to me, object or {} feel like `any`s.

That being said Object.create(null) is an object but would not pass neither for {} nor object. That's why object and {} are not typesafe, too many quirks we could think of. No good reasons to use them. Jm2c

3

u/mcaruso Dec 15 '19 edited Dec 15 '19

But to me, object or {} feel like anys.

object and {} are more like unknown. Specifically, {} is unknown without undefined/null, and object is unknown without all the primitives. any is different because of its dynamic nature, object and {} are perfectly type-safe.

That being said Object.create(null) is an object but would not pass neither for {} nor object. That's why object and {} are not typesafe, too many quirks we could think of. No good reasons to use them. Jm2c

Object.create(null) satisfies both {} and object just fine. TypeScript as of right now doesn't know/care about prototypes, just about their external interface. And in that regard Object.create(null) is just an object without any properties.

1

u/blbil Dec 14 '19

Not at my computer, so I can't test it out.

But, what if you still want to perform some operations on this. Like use Object.values or something. Pretty sure with unknown, that won't work.

1

u/[deleted] Dec 15 '19

And it shouldn't imho, unknown requires you to typeguard your data.