typescript 如何从Typescript中的函数获取参数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51851677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get argument types from function in Typescript
提问by ABMagil
I may have missed something in the docs, but I can't find any way in typescript to get the types of the parameters in a function. That is, I've got a function
我可能在文档中遗漏了一些东西,但我在打字稿中找不到任何方法来获取函数中参数的类型。也就是说,我有一个功能
function test(a: string, b: number) {
console.log(a);
console.log(b)
}
I want access to the types string
and number
, likely as a tuple.
我想访问类型string
和number
,可能是一个元组。
I know I can get the type of the function itself, as typeof test
, or the return type via ReturnType<test>
.
我知道我可以通过 .as 获取函数本身的类型typeof test
或返回类型ReturnType<test>
。
When I tried keyof typeof test
, it returned never
, which I also couldn't explain.
当我尝试时keyof typeof test
,它返回了never
,我也无法解释。
Other answers like this onepoint to extends
, but I don't really understand how that works and don't give me an easy way to access the set-of-all-params as a type.
像这样的其他答案指向extends
,但我真的不明白它是如何工作的,并且没有给我一种简单的方法来访问 set-of-all-params 作为一种类型。
回答by jcalz
Typescript now comes with a predefined Parameters<F>
type alias in the standard librarywhich is almost the same as ArgumentTypes<>
below, so you can just use that instead of creating your own type alias.
Typescript 现在在标准库中带有一个预定义的Parameters<F>
类型别名,它与ArgumentTypes<>
下面几乎相同,因此您可以使用它而不是创建自己的类型别名。
type TestParams = Parameters<(a: string, b: number) => void> // [string, number]
Then to get for example the second parameter's type you can use the numeric indexing operator:
然后要获取例如第二个参数的类型,您可以使用数字索引运算符:
type SecondParam = TestParams[1] // number
Original answer:
原答案:
Yes, now that TypeScript 3.0 has introduced tuples in rest/spread positions, you can create a conditional type to do this:
是的,现在 TypeScript 3.0在 rest/spread 位置引入了元组,你可以创建一个条件类型来做到这一点:
type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any ? A : never;
Let's see if it works:
让我们看看它是否有效:
type TestArguments = ArgumentTypes<typeof test>; // [string, number]
Looks good. Note that these beefed-up tuples also capture things like optional parameters and rest parameters:
看起来挺好的。请注意,这些增强的元组还捕获了诸如可选参数和其余参数之类的内容:
declare function optionalParams(a: string, b?: number, c?: boolean): void;
type OptionalParamsArgs = ArgumentTypes<typeof optionalParams>;
// [string, (number | undefined)?, (boolean | undefined)?]
declare function restParams(a: string, b: number, ...c: boolean[]): void;
type RestParamsArgs = ArgumentTypes<typeof restParams>;
// [string, number, ...boolean[]]
Hope that helps. Good luck!
希望有帮助。祝你好运!
回答by HugoTeixeira
A possible solution is to use the arguments
variable (which is a local variable accessible within all functions and contains an entry for each argument passed to that function). So you can do:
一个可能的解决方案是使用arguments
变量(它是所有函数中都可以访问的局部变量,并且包含传递给该函数的每个参数的条目)。所以你可以这样做:
const args = Array.prototype.slice.call(arguments, 0, arguments.length);
const argTypes = args.map(e => typeof e);
console.log(argTypes);
This prints:
这打印:
["string", "number"]