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

Requesting support for variadics with varargs in arbitrary but contiguous positions in .d.ts files #3686

Closed
aholmes opened this issue Jun 30, 2015 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@aholmes
Copy link

aholmes commented Jun 30, 2015

Some libraries with variadic methods that use varargs don't always place the parameters at the end of the parameter list. One such example is lodash's assign method whose signature and API documentation is as follows.

_.assign(object, [sources], [customizer], [thisArg])

  1. object (Object): The destination object.
  2. sources: The source objects.
  3. customizer: The function to customize assigned values.
  4. thisArg: The this binding of customizer.

TypeScript does not currently support this use case, and will report the error "A rest parameter must be last in a parameter list."

A way around this is to write an arbitrary number of method declarations (which is what lodash's .d.ts file does); one for each possible length of varargs. As I'm sure is clear, the arity being infinite, there is a concrete limit to how many of these declarations can exist. It also unfortunately "dirtys" the type hinting, making the method look more complex than it is. Lastly, maintaining these kinds of declarations is prone to error.

Edit:

Having thought about this a bit more, I realize the issue may actually lie with the .d.ts file. The documentation for writing declaration files recommends that optional parameters are not supplied as part of a function declaration. A possible fix, then, is to write another declaration with the varargs, and one without.

@danquirk
Copy link
Member

We have had previous requests for this (not requiring optional/rest params to be last) but I cannot find them via search at the moment...

@DanielRosenwasser
Copy link
Member

#1024 seems vaguely related. Maybe that's what you're looking for @danquirk

@danquirk
Copy link
Member

No I think it was a specific request for optional parameters in the middle of the param list due to some particular JavaScript APIs doing this.

@aholmes
Copy link
Author

aholmes commented Jun 30, 2015

Is it #1360?

@danquirk
Copy link
Member

That looks like it, thanks. Will close this as a dupe unless there's something unique to your request that isn't captured there.

@danquirk danquirk added the Duplicate An existing issue was already created label Jun 30, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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