Tuples as types for rest ...arguments
g162h3 opened this issue · 4 comments
g162h3 commented
This feature could be used in observable-like generic classes/interfaces:
interface EventSource<Args extends any[]> {
observe(listener: (...args: Args)): void;
map<T>(mapping: (...args: Args) => T): EventSource<[T]>;
}Currently the only way I know of is to fallback to any.
RyanCavanaugh commented
Could you write up a bit longer explanation of how you'd want this to work? For example, if the declaration of map was instead map<T>(x: T, mapping: (...args: Args) => T, y: T): EventSource<[T]>;, what would the result be?
sandersn commented
d180cf commented
@RyanCavanaugh, I suppose you meant this signature: mapping: (...args: Args, y: T) => T. If es6 doesn't allow named args after ...args, then tsc can do the same.
eggers commented
Another use case for this is to properly declare the spread function. Something like this:
interface Promise<T> {
spread<U>(fulfilledHandler: (...values: T & any[]) => U | Promise<U>): Promise<U>;
spread<U>(fulfilledHandler: (...values: [T]) => U | Promise<U>): Promise<U>;
}