-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already createdFixedA PR has been merged for this issueA PR has been merged for this issue
Description
Is it possible to add type info to the rest parameters in a way that each individual parameter can have a different type?
This works, but with this there can be any number parameters, and all have to have the same type:
function myFunction<T>(...args:T[]) {
}
myFunction<number>(1, 3);
It would be really useful if I could force the exact number of arguments and the types for each one, but all this within generics, so something like this (but this is obviously syntactically wrong):
function myFunction<T>(...args:T) {
}
interface MyParams {
[0]:string;
[1]:number;
}
myFunction<MyParams>("a", 1)
An example use case is writing an observer class that the user could specify the arguments of the listener functions for:
class Observer<T> {
addListener(...args:T) {
}
}
If this is not supported, what do you recommend using instead? I can use any[] of course, or living with the constraint of having a fixed number of fixed typed parameters.
WuTheFWasThat, AJamesPhillips, mnpenner, kaleb, ssynix and 3 more
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already createdFixedA PR has been merged for this issueA PR has been merged for this issue