[英]how to map enum values to type in typescript
I have an enum type which looks this:我有一个看起来像这样的枚举类型:
export enum API_TYPE {
INDEX = "index_api",
CREATE = "create_api",
SHOW = "show_api",
UPDATE = "update_api",
DELETE = "destroy_api"
};
now, I have a function which takes a number and api_type parameter现在,我有一个 function 需要一个数字和 api_type 参数
export function abc (id: number, api_type:?) =>
for this function, possible values for api_type would be values of keys of enum api_type
对于此 function,api_type 的可能值将是枚举api_type
的键值
What is the best way to discuss type of api_type
parameter of function abc
?讨论function abc
的api_type
参数类型的最佳方法是什么?
One way would be to export type Api_Type = "index_api" | "create_api" | "show_api" | "update_api" | "destroy_api"
一种方法是export type Api_Type = "index_api" | "create_api" | "show_api" | "update_api" | "destroy_api"
export type Api_Type = "index_api" | "create_api" | "show_api" | "update_api" | "destroy_api"
and then接着
export function abc (id: number, api_type:Api_Type) =>
but this way if I add new property (key) to enum API_TYPE {
i would also have to remember to manually add it to Api_Type
但是这样,如果我将新属性(键)添加到enum API_TYPE {
我还必须记住手动将其添加到Api_Type
Is there a way, I can map enum value to a type?有没有办法,我可以将 map 枚举值转为一个类型? or a better way to do this?或者更好的方法来做到这一点?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.