Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow inference of the type of the rest parameters. #24240

Closed
rzvc opened this issue May 18, 2018 · 3 comments
Closed

Allow inference of the type of the rest parameters. #24240

rzvc opened this issue May 18, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@rzvc
Copy link

rzvc commented May 18, 2018

Search Terms

rest parameters infer conditional types

Suggestion

Allow type inference for rest parameters:

type ArgumentsType<T extends (...args: any[]) => any> = T extends (...args: infer R) => any ? R : any[];

Currently the compiler complains that rest parameters have to be an array type, but if the full type of the rest parameters could be inferred (as a tuple), then you could extract the type of the parameters of a function, allowing you to do something like this:

function f(a: number, b: string) : void
{
}

let params : ArgumentsType<typeof f> = [10, 'test'];

Furthermore, allowing the rest parameters to have a type, would then allow typed forwarding:

function f2(extra: bool, ...rest: ArgumentsType<typeof f>) : void
{
	f(...rest);
}
@s-ve
Copy link

s-ve commented May 18, 2018

If I'm not mistaking, #5453 already covers your suggestion 😊

@rzvc
Copy link
Author

rzvc commented May 18, 2018

@s-ve Thanks for that. It looks like it does cover this usage, but my suggestion should be far easier to implement, because it doesn't require an additional kind of types.

Also, I'm not sure variadic kinds are needed to specify a tuple, since the type of rest parameters is already a tuple, so T should be enough (instead of ...T), as long as T is a tuple. They would be needed however to specify types of arbitrary length.

// For example, you don't need variadic kinds for this:
function f(...rest: [number, string]) : void;

// But you need for this:
function f(...rest: [number, string, ...boolean]) : void;

With the above examples in mind, it becomes obvious that you don't need variadic kinds for the type inference of rest parameters, because at the time when the inference is done, the tuple is already known.

Edit, I'd also like to link this here, which proposes a similar thing: #5331

@ghost ghost added the Duplicate An existing issue was already created label May 18, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Aug 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants