[英]Typeof arguments object in TypeScript
I basically have this: 我基本上有这个:
function foo(){
// literally pass the arguments object into the pragmatik.parse method
// the purpose of pragmatik.parse is to handle more complex variadic functions
const [a,b,c,d,e] = pragmatik.parse(arguments);
// now we have the correct arguments in the expected location, using array destructuring
}
so we have the pragmatik.parse
method: 所以我们有
pragmatik.parse
方法:
function parse(args){
// return parsed arguments
}
now I want to use TypeScript to define types, all I know is that arguments is an Object: 现在我想使用TypeScript来定义类型,我所知道的是参数是一个Object:
function parse(args: Object){
}
so my question is: does TypeScript give a definition or type for an arguments
object in JS? 所以我的问题是:TypeScript是否为JS中的
arguments
对象提供了定义或类型? Sorry this is a bit meta, but please bear with me, what I am asking about is sane. 对不起,这有点元,但请耐心等待,我所要求的是理智。
My Webstorm IDE suggests that this might be IArguments
, which is provided by: lib/es6/d.ts
, which is somewhere out there. 我的Webstorm IDE建议这可能是
IArguments
,它由: lib/es6/d.ts
,它位于某处。 Maybe someone can verify this is correct, but I am fairly certain. 也许有人可以证实这是正确的,但我相当肯定。
So the answer would be: 所以答案是:
function parse(args: IArguments){
}
and the full signature would be: 并且完整的签名将是:
function parse(args: IArguments) : Array<any> {
}
since the parse method returns a generic array 因为parse方法返回一个通用数组
You can pick exact type of arguments with tsargs package from npm 你可以从npm中选择tsargs包的确切类型的参数
Eg: 例如:
import { ArgsN } from 'tsargs';
function foo(a: boolean, b: number, c: string) {}
const argsABC: ArgsN<typeof foo> = [ true, 123, 'Hello' ];
In your case: 在你的情况下:
import { ArgsN } from 'tsargs';
function parse<T extends (...args: any[]) => any>(args: IArguments): ArgsN<T> {
// return parsed arguments
}
// ...
const args = parse<typeof foo>(arguments);
// args -> [ boolean, number, string ];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.