Typescript Pick 用于字面量类型

huangapple go评论82阅读模式
英文:

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.

Playground.

type Roles = &#39;admin&#39; | &#39;user&#39; | &#39;guest&#39;

// no connection to Roles
type ApiRoute = { roles: &#39;admin&#39; | &#39;user&#39; }

// how to?
type ApiRouteWithCheck = { roles: Pick&lt;Roles, &#39;admin&#39; | &#39;user&#39;&gt; }

答案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&lt;Roles, &#39;guest&#39;&gt; };

or Extract the values you do:

type ApiRoute = { roles: Extract&lt;Roles, &#39;admin&#39; | &#39;user&#39;&gt; };

答案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 = &#39;admin&#39;, user = &#39;user&#39;, guest = &#39;guest&#39; }

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 = &#39;admin&#39; | &#39;superadmin&#39;
export type UserRoles = &#39;user&#39;
export type PublicRoles = &#39;guest&#39;

export type AllRoles = AdminRoles | UserRoles | PublicRoles
export type AuthRoles = Exclude&lt;AllRoles, PublicRoles&gt;

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 = &#39;admin&#39; | &#39;superadmin&#39;
export type ModeratorRoles = &#39;moderator&#39; | &#39;supermoderator&#39;
export type UserRoles = &#39;user&#39; | &#39;superuser&#39;
export type PublicRoles = &#39;guest&#39;

export type AllRoles = AdminRoles | UserRoles | PublicRoles | ModeratorRoles
export type AuthRoles = Exclude&lt;AllRoles, PublicRoles&gt;

huangapple
  • 本文由 发表于 2020年1月3日 22:08:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定