![](/img/trans.png)
[英]Do I have to use an interface to define the elements of an object with typescript?
[英]Define an object with multiple elements using TypeScript
我想使用 TypeScript 作為此代碼:
const arr = [
{test:1,
name:5,
car:8
}
]
const a = arr.map((i: {test:number, car: number, name: number}) => i.test)
因此,要在map()
function 中定義i
類型,我必須編寫{test:number, car: number, name: number}
,但是存在一個替代方法來定義i
具有 Object 類型而不定義i
中的每個元素?
你必須定義一個接口
interface MyInterface {
test: number,
name:number,
car:number
}
const arr: MyInterface[] = [
{
test:1,
name:5,
car:8
}
];
const a = arr.map( i => i.test)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.