microsoft/TypeScript

Tuples as types for rest ...arguments

g162h3 opened this issue · 4 comments

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.

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?

This sounds a lot like #4130. I have a proposal up at #5296, but I think it's currently too narrow for your use. I am definitely interested in seeing a good real-world explanations of how you (and others) would like it to work.

@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.

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>;
}