我试图用接口破坏分配,但是不能这样写。
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'
答案 0 :(得分:2)
只需在变量上声明类型,而无需使用怪异的对象符号:
const { ...foo }: TYPE = e;
但是,这是一种复制对象的怪异方法-通常这样做是这样的:
const foo: TYPE = { ...e };