具有接口和rest属性的打字稿解构分配

时间:2019-08-04 13:08:49

标签: typescript ecmascript-6

我试图用接口破坏分配,但是不能这样写。

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'

0 个答案:

没有答案