Skip to content

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

Closed
@aholmes

Description

@aholmes

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions