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
  • 539,169
  • 125
  • 977
  • 1,137
st_clair_clarke
  • 5,003
  • 12
  • 43
  • 72

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
  • 572,313
  • 128
  • 898
  • 1,281