typescript destructuring assignment with interface

2019-08-19 18:07发布

问题:

I tried to destructuring assignment with interface, but cannot write like this.

interface TYPE {
  id?: number;
  type?: string;
}

const e =  {
  'id': 123,
  'type': 'type_x',
  'other1': 'other_x1',
  'other2': 'other_x2'
}
const {...foo, ...others}: {foo: TYPE, others: any} = e;
console.log(foo.id, foo.type) // expected: 123, 'type_x'
console.log(others.other1, others.other2) // expected: other_x1, 'other_x2'