Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between {} and Object?

Recently, I'm confused about the difference between {} and Object. Sometimes, {} will solve my problem, sometimes, it can't and I switched with Object. I really don't know why.

I did some tests, hopefully, it can give you some hint.

const t: Array<{label: string}> = [{label:'1'}];
const arr: Array<{}> = t; //error
const arr2: Array<Object> = t; //pass
like image 750
ben.liu Avatar asked Jan 11 '18 07:01

ben.liu


Video Answer


1 Answers

{} is an alias for new Object().

So you can say Object is a class and {} is an instance of that class.

You can see here:

console.log(JSON.stringify(new Object()) == JSON.stringify({}))

console.log({} instanceof Object)
like image 135
Ayush Gupta Avatar answered Oct 14 '22 08:10

Ayush Gupta