enum EmailStatus {
Read = 'READ',
Unread = 'UNREAD',
Draft = 'DRAFT',
}
interface Status {
// ⛔️ Error: A mapped type may not declare properties or methods.ts(7061)
[key in EmailStatus]: string;
}
改用 type :
enum EmailStatus {
Read = 'READ',
Unread = 'UNREAD',
Draft = 'DRAFT',
}
// 👇️ use type alias
type Status = {
[key in EmailStatus]: string;
};
文章讨论了在TypeScript中如何使用enum和type来声明状态枚举及其映射的接口。出现了一个错误,提示mapped类型不能声明属性或方法,解决方案是改用type关键字来创建一个对象类型别名。
7124

被折叠的 条评论
为什么被折叠?



