我有这个枚举
enum methods {
DELETE = 1,
POST,
PUT,
GET
}
Run Code Online (Sandbox Code Playgroud)
我想要一个函数接受一个参数,该参数可以是methods枚举的键之一。
const getMethodPriority = (method /* how do I type this? */) => {
return methods[method];
};
Run Code Online (Sandbox Code Playgroud)
所以,例如getMethodPriority("DELETE")会返回1。
如何输入method参数?
Pav*_*kov 10
您可以通过将其值转换为 来直接从数字枚举中获取数字number:
enum methods {
DELETE = 1,
POST = 2,
PUT = 3,
GET = 4
}
let enumPriority = methods.DELETE as number;
// enumPriority == 1
Run Code Online (Sandbox Code Playgroud)
但如果你真的想有一个方法,你可以:
enum methods {
DELETE = 1,
POST = 2,
PUT = 3,
GET = 4
}
const getMethodPriority = (method: keyof typeof methods) => {
return methods[method];
};
// how to call
getMethodPriority("DELETE");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2765 次 |
| 最近记录: |