You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
The text was updated successfully, but these errors were encountered:
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])
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.
The text was updated successfully, but these errors were encountered: