typescript - 使用打字稿接口将数组从一种类型映射到另一种类型
问题描述
我尝试将数组从一种接口类型映射到另一种接口类型,我的代码如下所示:
interface Entry { ... }
return this.dataService.getData(id).pipe(
map(data => ({
...data,
array: data.array.map(entry => {
if (
instanceOfEntryB(entry) ||
entry.options === undefined
) {
return entry;
} else {
return <Entry & { options: SelectItem[] }>{
...entry,
options: entry.options.map<SelectItem>(
option => ({
description: option.label,
value: option.key,
selected: false
})
)
};
}
})
})),
tap(({ array }) => {
ctx.patchState({ array });
})
);
data.array
键入为Entry & { options?: Option[] }
并且array
在州内为Entry & { options?: SelectItem[] }
。ctx.patchState
在for 中, linterarray
抛出一个错误:Type 'Option' is missing the following properties from type 'SelectItem': value, selected, description `
我怎样才能以最聪明的方式解决这个问题?
解决方案
推荐阅读
- node.js - Nodemon app cannot find module when restarting
- javascript - 如何确定发生鼠标悬停事件的数组的哪个索引?
- azure - 通过 Azure DevOps 将 SAS 令牌部署到 Azure Key Vault
- python - Checking row-by-row if a value in one column is present as a substring of a value in another column, appending the string if the boolean value = False
- python-3.x - finding a file using general location in a python script
- python - Can't deal with path
- r - 如何计算布朗桥运动模型的面积?
- python - How to Change x-axis to logarithmic in PLOTLY histogram
- c# - 如何在 .NetCore 中使用 GetWebRequest 方法
- azure-blob-storage - Cannot use azcopy from one blob to another with SAS