![](/img/trans.png)
[英]Property does not exists on type when destructuring assignment and 'Pick' in Typescript
[英]typescript destructuring assignment with interface
我試圖用接口破壞分配,但是不能這樣寫。
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 }: TYPE = e;
但是,這是一種復制對象的怪異方法-通常這樣做是這樣的:
const foo: TYPE = { ...e };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.