Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...