我有这个枚举
enum methods {
DELETE = 1,
POST,
PUT,
GET
}
我想要一个函数接受一个参数,该参数可以是methods
枚举的键之一。
const getMethodPriority = (method /* how do I type this? */) => {
return methods[method];
};
所以,例如getMethodPriority("DELETE")
会返回1
。
如何输入method
参数?
您可以通过将其值转换为 来直接从数字枚举中获取数字number
:
enum methods {
DELETE = 1,
POST = 2,
PUT = 3,
GET = 4
}
let enumPriority = methods.DELETE as number;
// enumPriority == 1
但如果你真的想有一个方法,你可以:
enum methods {
DELETE = 1,
POST = 2,
PUT = 3,
GET = 4
}
const getMethodPriority = (method: keyof typeof methods) => {
return methods[method];
};
// how to call
getMethodPriority("DELETE");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句