0

I have the following:

const {title} = {title: "Menu"};

alert( title ); // Menu

Now, if I have the following:

interface IEntry {title: string}
class Entry { title = 'Xoom' }

entry:IEntry = new Entry()

Why doesn't the following work, and what is the correct form?

const {title: this.entry.title} = {title: "Menu"}

I was expecting the title property value to be assigned to this.entry.title.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
st_clair_clarke
  • 5,453
  • 13
  • 49
  • 75

1 Answers1

1

You can't use a const declaration if you don't want to declare a new variable but destructure onto an existing object. The correct syntax would be

({title: this.entry.title} = {title: "Menu"});

See also Object destructuring without var, let or const.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375