5

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:

function parse(args){

   // return parsed arguments

}

now I want to use TypeScript to define types, all I know is that arguments is an Object:

function parse(args: Object){


}

so my question is: does TypeScript give a definition or type for an arguments object in JS? Sorry this is a bit meta, but please bear with me, what I am asking about is sane.

2 Answers 2

1

My Webstorm IDE suggests that this might be IArguments, which is provided by: lib/es6/d.ts, which is somewhere out there. 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

Sign up to request clarification or add additional context in comments.

Comments

1

You can pick exact type of arguments with tsargs package from npm

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 ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.