首页 > 解决方案 > 确保静态键存在于另一个类上

问题描述

我正在寻找一种方法来确保一个类的所有属性都存在于另一个类中。

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}

标签: typescript

解决方案


这适用于常规对象:

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,
}

推荐阅读