typescript - 确保静态键存在于另一个类上
问题描述
我正在寻找一种方法来确保一个类的所有属性都存在于另一个类中。
export class Components {
static [Pages.home] = Home.Page
static [Pages.login] = Login.Page
static [Pages.logout] = Logout.Page
static [Pages.dashboardManage] = DashboardManage.Page
static [Pages.dashboardOrganizationTable] = DashboardOrganizationTable.Page
static [Pages.organizationSettings] = OrganizationSettings.Page
}
export class Resolvers implements Components {
static [Pages.home] = Home.Page
static [Pages.login] = Login.Page
static [Pages.logout] = Logout.Page
static [Pages.dashboardManage] = DashboardManage.Page
static [Pages.dashboardOrganizationTable] = DashboardOrganizationTable.Page
static [Pages.organizationSettings] = OrganizationSettings.Page
}
implements Components
不工作
我也试过这个:
type PagesInUse = keyof typeof Components
type ExmapB = {[key in PagesInUse]: any}
解决方案
这适用于常规对象:
export const Components = {
[Pages.home]: Home.Page,
[Pages.login]: Login.Page,
[Pages.logout]: Logout.Page,
[Pages.dashboardManage]: DashboardManage.Page,
[Pages.dashboardOrganizationTable]: DashboardOrganizationTable.Page,
[Pages.organizationSettings]: OrganizationSettings.Page,
}
type PageKeysInUse = keyof typeof Components
type PagesInUse = { [key in PageKeysInUse]: any }
export const Resolvers: PagesInUse = {
[Pages.home]: Home.Page,
[Pages.login]: Login.Page,
[Pages.logout]: Logout.Page,
[Pages.dashboardManage]: DashboardManage.Page,
[Pages.dashboardOrganizationTable]: DashboardOrganizationTable.Page,
[Pages.organizationSettings]: OrganizationSettings.Page,
}
推荐阅读
- android - 数据库架构未更改时收到“房间无法验证数据完整性”错误消息
- angular - 从 Angular 项目中卸载包
- notifications - Twilio - 如何在所有子账户中使用全球 twilio 号码(已经为主账户购买了 twilio 号码)
- github-actions - 如何在程序上命名工件?
- go - 去比较两个结构是否大于
- javascript - React - 从子组件的外部调用父函数
- git - 如何在远程服务器上运行 Satis?
- javascript - node.js ejs包含partials,并且在main中,找不到partial文件中定义的变量
- r - 在 r 中形成块对角矩阵
- nearprotocol - 在最后一个 RocksDB 版本碰撞后无法编译近核项目