Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

typescript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...