【发布时间】:2019-12-12 03:15:01
【问题描述】:
我尝试用接口解构赋值,但不能这样写。
interface TYPE {
id?: number;
type?: string;
}
const e = {
'id': 123,
'type': 'type_x',
'other': 'other_x'
}
const {...foo}: {foo: TYPE} = e;
console.log(foo.id, foo.type) // expected: 123, 'type_x'
【问题讨论】:
-
您希望
const {...foo}: {foo: TYPE} = e;究竟会做什么? -
const {...foo, ...rest}: {foo: TYPE, rest: any} = e,;析构分配类型,并休息属性,但不要这样写。
标签: javascript typescript object ecmascript-6