Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between the empty object type and Object?

I've noticed that TypeScript supports using the following types:

  • {} (referred to in the specs as Empty Object Type)
  • Object

They both seem to be equivalent and interchangeable as far as I can tell. What are the differences between them?

like image 231
Sam Avatar asked Jan 28 '14 03:01

Sam


1 Answers

Within the context of TypeScript, there is no practical difference but there is a semantic difference. All the members of the Object are implicitly present on all objects.

{} means something that has no members of its own. {} would still have all the members of Object. So they are interchangeable in TypeScript.

// Extend ALL objects
interface Object{
    baz:number;
}

var foo:{} = {};
var bar:Object = {};

foo.baz = 123;
bar.baz = 123;

Personally I haven't ever declared a variable to be one of these. Perhaps you should use any which is something that is compatible with everything.

like image 92
basarat Avatar answered Nov 10 '22 02:11

basarat