基于反应界面生成空对象
我的界面是在枚举上生成的,并且它
enum HealthPlanStatus {
InProgress = 'InProgress',
Completed = 'Completed',
}
export type THealthPlanCardsByStatus = {
[status in keyof typeof HealthPlanStatus]: {
cards: string[];
};
}
在项目中具有可读的键,我想正确声明由 healthplanstatus 生成的空对象。
const cards = ['1', '2', '3']
const cardsGroupedByStatus: any = {}
Object.values(HealthPlanStatus).forEach(status => {
cardsGroupedByStatus[status] = cards.filter(card => card)
})
为了避免错误,它现在具有“任何” ,但是我想看到任何一个变体:
- const cardgroupedbystatus:thealthplandarcardsbystatus = {}
- const const carst groupedbystatus = {}作为thealthplancardsbystatus
在这两种情况下我都有不同的错误:
- < em> const cardsgroupedbystatus:thealthplancardsbystatus 类型'{}'丢失了类型'thealthplancardsbystatus'的以下属性:inprogress,已完成
- 无法分配给'inprogress'/'已完成',因为它是无读的
,可能,请给我建议如何正确地对新生成对象声明。 谢谢!
I have interface that is generated base on enum and it has readonly keys
enum HealthPlanStatus {
InProgress = 'InProgress',
Completed = 'Completed',
}
export type THealthPlanCardsByStatus = {
[status in keyof typeof HealthPlanStatus]: {
cards: string[];
};
}
In my project I want to correctly declare an empty object that is generated by HealthPlanStatus.
const cards = ['1', '2', '3']
const cardsGroupedByStatus: any = {}
Object.values(HealthPlanStatus).forEach(status => {
cardsGroupedByStatus[status] = cards.filter(card => card)
})
To avoid errors it has now "any", but I want to see any of this variants:
- const cardsGroupedByStatus: THealthPlanCardsByStatus = {}
- const cardsGroupedByStatus = {} as THealthPlanCardsByStatus
In both cases I have different errors:
- const cardsGroupedByStatus: THealthPlanCardsByStatus
Type '{}' is missing the following properties from type 'THealthPlanCardsByStatus': InProgress, Completed - Cannot assign to 'InProgress' / 'Completed' because it is a read-only
If it's not possible, please, give me advise how to correctly make declaration for new generates object.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于注释:
typeof
,将其删除并离开:或
Based on comments:
keyof typeof
, remove it and leave just:or