簡體   English   中英

帶接口的打字稿解構分配

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM