英文:
Typescript Pick for literal type
问题
Here is the translated code portion:
type Roles = 'admin' | 'user' | 'guest'
// no connection to Roles
type ApiRoute = { roles: 'admin' | 'user' }
// how to?
type ApiRouteWithCheck = { roles: Pick<Roles, 'admin' | 'user'> }
Please note that I've only translated the code and not provided additional information.
英文:
Is there a way to Pick (or any other solution) from literal type for connection to main Roles interface?
Reason for this, if i in future will change or delete something in Roles, i can see where i need to change/delete it to.
type Roles = 'admin' | 'user' | 'guest'
// no connection to Roles
type ApiRoute = { roles: 'admin' | 'user' }
// how to?
type ApiRouteWithCheck = { roles: Pick<Roles, 'admin' | 'user'> }
答案1
得分: 20
你可以要么 Exclude
你不想要的值:
type ApiRoute = { roles: Exclude<Roles, 'guest'> };
或者要么 Extract
你想要的值:
type ApiRoute = { roles: Extract<Roles, 'admin' | 'user'> };
英文:
You can either Exclude
the values you don't want:
type ApiRoute = { roles: Exclude<Roles, 'guest'> };
or Extract
the values you do:
type ApiRoute = { roles: Extract<Roles, 'admin' | 'user'> };
答案2
得分: 1
将Roles
更改为一个enum
。这样,在使用其值的地方必须使用其名称。<br>
当您向Roles
添加新值时,您可以使用“在文件中查找”(或VSCode的“代码镜头”功能)查找它在哪里被使用。
enum Roles { admin = 'admin', user = 'user', guest = 'guest' }
type ApiRoutePublic = { roles: Roles }
type ApiRouteAuth = { roles: Roles.admin | Roles.user }
type ApiRouteAdmin = { roles: Roles.admin }
英文:
Change Roles
to be an enum
. This way its name must be used where its values are used.<br>
When you add new values to Roles
you can use "find in files" (or the "code lens" feature of VSCode) to find out where it is used.
enum Roles { admin = 'admin', user = 'user', guest = 'guest' }
type ApiRoutePublic = { roles: Roles }
type ApiRouteAuth = { roles: Roles.admin | Roles.user }
type ApiRouteAdmin = { roles: Roles.admin }
答案3
得分: 0
另一种方法是将其分成不同的类型并根据这些类型导出类型,这样,如果将来添加新的类型,只需将它们添加到AllRoles
中,如果需要使用其中一种类型,可以直接导入它们,而不是选择/排除它们。
export type AdminRoles = 'admin' | 'superadmin'
export type UserRoles = 'user'
export type PublicRoles = 'guest'
export type AllRoles = AdminRoles | UserRoles | PublicRoles
export type AuthRoles = Exclude<AllRoles, PublicRoles>
现在,将来如果添加另一种角色类型,只需将其添加到AllRoles
中,因为我们在排除PublicRoles
。
export type AdminRoles = 'admin' | 'superadmin'
export type ModeratorRoles = 'moderator' | 'supermoderator'
export type UserRoles = 'user' | 'superuser'
export type PublicRoles = 'guest'
export type AllRoles = AdminRoles | UserRoles | PublicRoles | ModeratorRoles
export type AuthRoles = Exclude<AllRoles, PublicRoles>
英文:
Another way is to split it up into different types and export types based on those types, this way if you add new types in the future you just add them to AllRoles
and if you need to use one of those types you can just import them directly instead of picking/excluding them
export type AdminRoles = 'admin' | 'superadmin'
export type UserRoles = 'user'
export type PublicRoles = 'guest'
export type AllRoles = AdminRoles | UserRoles | PublicRoles
export type AuthRoles = Exclude<AllRoles, PublicRoles>
Now in the future if you add another type of role you can just add it to AllRoutes
because we are excluding PublicRoles
export type AdminRoles = 'admin' | 'superadmin'
export type ModeratorRoles = 'moderator' | 'supermoderator'
export type UserRoles = 'user' | 'superuser'
export type PublicRoles = 'guest'
export type AllRoles = AdminRoles | UserRoles | PublicRoles | ModeratorRoles
export type AuthRoles = Exclude<AllRoles, PublicRoles>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论