繁体   English   中英

如何使用打字稿从array.Map 返回一个类?

[英]How to return a class from array.Map with typescript?

如何使底部语句返回 StoreModel 数组? 接口和映射数组之间的模型和存储的签名匹配。

interface StoreModels {
    model: string | undefined;
    store: string;
}

const proposedStoreValues = proposedStores.map(({ model, store }) => ({ model, store }));
const proposedStoreValues = proposedStores.map(
  ({model, store}: StoreModels) => ({
    model,
    store
  })
)

如评论中所示,您只需在箭头函数中添加StoreModels的签名:

interface StoreModels {
    model: string | undefined;
    store: string;
}

const proposedStoreValues = proposedStores.map(({ model, store }): StoreModels => ({ model, store }));

参考: https : //www.typescriptlang.org/docs/handbook/functions.html

您可以将返回类型添加到这样的箭头函数中: (): ReturnVal => something

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM