Repeat parameter groups (including non-last rest parameter) #6755
Labels
Suggestion
An idea for TypeScript
Too Complex
An issue which adding support for may be too complex for the value it adds
There are many JS libraries out there that take arguments in the form:
When writing a
d.ts
declaration file for them, there we can encounter many issues:If all of the parameters are of the same type, say
number
it is trivial to describe using rest:This already feels a bit hacky, since someone could provide an odd number of parameters (missing the last Y) and the type-checking would not spot the error.
On the other hand if the repeated parameters are of different type, or happen not to be at the end of the definition, there is currently no way to describe such methods, i.e.:
For these types of methods to behave properly, the type definitions need to be even hackier (assuming X needs to be an interface of type XInterface and Y needs to be an interface of type YInterface and options is an object):
This works, but is really ugly and doesn't provide necessary type safety, since I could pass
XInterface
to aYInterface
parameter, or even theoptions
interface to anXInterface
parameter.There should be a way to define "argument repeat groups" that could behave the same way as
...rest
parameters, but for multiple parameters at once, and could be placed anywhere in the method definition (not only at the end).For real life use of such object parameter patterns, take a look at RethinkDB's JavaScript API.
Methods such as map() have their syntax specified as:
Parameters in the brackets are optional, while the
...
means there can be more of those.A "non-last rest" parameter for type declarations would solve 90% of these problems, so it could be a good start. Repeated groups of arguments are much rarer, but happen too, if you'd like to solve this issue in a more general way.
The text was updated successfully, but these errors were encountered: