0

I have seen typescript objects defined like this:

        const MyObj = {
          myVar1,
          name1: myVar2
        }

What is this object being defined here?

I understand that there is a key value pair here where name1 is the key, and myVar2 is the value. But what does it mean for there to be only a single value like myVar1?

(This is very hard to Google an answer for)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272

1 Answers1

-1

It's a shorthand declaration. There is some variable named myVar1 and this code says that on the object myObj, there will be a key with the name myVar1 with the value equalling the variable.

There are multiple shorthands for creating objects in TypeScript. Another is object deconstruction seen when you do this: const obj1 = {name: 'John'}; const obj2 = {...obj1}. This makes obj2 have all the same properties as obj1.