是否可以从对象中解构一个 属性 并将其分配给构造函数中的 属性 ?

Is it possible to destructure a property out of the object and assign it to a property on the constructor?

我正在使用打字稿并想从对象中解构属性。问题是我需要将它分配给 class:

的构造函数上的 属性
var someData = [{title: 'some title', desc: 'some desc'}];
var [{title}] = someData; // 'some title';

我想要更像的东西:

var [{title :as this.title$}] = someData;

这有可能以任何形状或形式出现吗?

是的,你可以这样做,但你需要删除声明符 (var),因为你正在解构为已经存在的东西。此外,as 是无效语法。删除它。

[{title: this.title$}] = someData;

一个完整的例子:

const someData = [
  { title: 'Destructuring' }
];

class A {
  title$: string;
  constructor() {
    [{ title: this.title$ }] = someData;
  }
}

TypeScript playground

Babel REPL

通过堆栈片段

const someData = [{
  title: 'Destructuring'
}];

class A {
  constructor() {
    [{title: this.title$}] = someData;
  }
}

console.log(new A());