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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With