[英]typescript destructuring assignment with interface
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',
'other': 'other_x'
}
const {...foo}: {foo: TYPE} = e;
console.log(foo.id, foo.type) // expected: 123, 'type_x'
Just declare the type on the variable, without that weird object notation: 只需在变量上声明类型,而无需使用怪异的对象符号:
const { ...foo }: TYPE = e;
That is a weird way to make a copy of an object however - it's usually done like so: 但是,这是一种复制对象的怪异方法-通常这样做是这样的:
const foo: TYPE = { ...e };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.