简体   繁体   English

类型别名“权限”循环引用自身

[英]Type alias 'Permission' circularly references itself

I am defining a type for my Permissions object which is:我正在为我的权限 object 定义一个类型,即:

const Permissions = {
    PageManagement: {
        MainPageManagement: {
            ViewPage: 'view-page',
            CreatePage: 'create-page',
            UpdatePage: 'update-page',
            DeletePage: 'delete-page',
            ActivateDeactivatePage: 'activate-deactivate-page',
        },
        PageSectionManagement: {
            ViewPageSection: 'view-page-section',
            CreatePageSection: 'create-page-section',
            UpdatePageSection: 'update-page-section',
            DeletePageSection: 'delete-page-section',
            ActivateDeactivatePageSection: 'activate-deactivate-page-section',
        },
    },
    RoleManagement: {
        ViewRole: 'view-role',
        CreateRole: 'create-role',
        UpdateRole: 'update-role',
        DeleteRole: 'delete-role',
        AssignRole: 'assign-role',
        RemoveRole: 'remove-role',
        ActivateDeactivateRole: 'activate-deactivate-role',
    },
};

As you can see, PageManagement has nested two objects, while RoleManagement doesn't have.如您所见, PageManagement嵌套了两个对象,而RoleManagement没有。 I'm trying to create a n-level nested type definition, the one that I've created now is:我正在尝试创建一个n-level嵌套类型定义,我现在创建的是:

type Permission = Record<string, string | Permission>

But it gives this error:但它给出了这个错误:

Type alias 'Permission' circularly references itself

Any idea what I'm doing wrong?知道我做错了什么吗? I'm using TypeScript v4.2.3我正在使用TypeScript v4.2.3

The Record is the source of the problem. Record是问题的根源。 It requires a complete type before it can result in a type.它需要一个完整的类型才能产生一个类型。

type Permission = { [key: string]: string | Permission }

This will work.这将起作用。

you can use an interface instead of type.您可以使用接口而不是类型。

const permissions: Permission = {
    PageManagement: {
        MainPageManagement: {
            ViewPage: 'view-page',
            CreatePage: 'create-page',
            UpdatePage: 'update-page',
            DeletePage: 'delete-page',
            ActivateDeactivatePage: 'activate-deactivate-page',
        },
        PageSectionManagement: {
            ViewPageSection: 'view-page-section',
            CreatePageSection: 'create-page-section',
            UpdatePageSection: 'update-page-section',
            DeletePageSection: 'delete-page-section',
            ActivateDeactivatePageSection: 'activate-deactivate-page-section',
        },
    },
    RoleManagement: {
        ViewRole: 'view-role',
        CreateRole: 'create-role',
        UpdateRole: 'update-role',
        DeleteRole: 'delete-role',
        AssignRole: 'assign-role',
        RemoveRole: 'remove-role',
        ActivateDeactivateRole: 'activate-deactivate-role',
    },
};

interface Permission {
  [key: string]: string | Permission
}

Also reproduction example也是复制示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型别名循环引用自身 - Type alias circularly references itself TypeScript 类型别名循环引用自身 - TypeScript Type alias circularly references itself Redux 操作“类型别名'操作'循环引用自身” - Redux Action “Type alias 'Action' circularly references itself” ts-toolbelt 在 node_modules 上给出错误。 类型别名 &#39;MergeList&#39; 循环引用自身 - ts-toolbelt giving errors on node_modules. Type alias 'MergeList' circularly references itself 如何修复此错误:错误 TS2456:类型别名“几何”循环引用自身? - How to fix this error: error TS2456: Type alias 'Geometry' circularly references itself? 如何解决“类型循环引用自身错误”? - How to resolve “Type circularly references itself error”? 如何在Typescript中声明递归Map类型(TS2456:Type alias&#39;*&#39;循环引用自身。) - How do I declare a recursive Map type in Typescript (TS2456: Type alias '*' circularly references itself.) TypeScript: node_modules/classnames/index.d.ts:13:13 类型别名'Argument'循环引用自身 - TypeScript: node_modules/classnames/index.d.ts:13:13 Type alias 'Argument' circularly references itself 循环引用对象的问题 NestedKeyof 类型 - problem NestedKeyof type with circularly references objects 如何在 TypeScript 中创建循环引用类型? - How to create a circularly referenced type in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM