代码之家  ›  专栏  ›  技术社区  ›  ThomasReggi

确保静态键存在于另一个类上

  •  0
  • ThomasReggi  · 技术社区  · 5 年前

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

    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}
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   ThomasReggi    5 年前

    这适用于常规对象:

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