Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuples as types for rest ...arguments #5331

Closed
g162h3 opened this issue Oct 19, 2015 · 4 comments · Fixed by #24897
Closed

Tuples as types for rest ...arguments #5331

g162h3 opened this issue Oct 19, 2015 · 4 comments · Fixed by #24897
Assignees
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@g162h3
Copy link

g162h3 commented Oct 19, 2015

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 RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Oct 19, 2015
@RyanCavanaugh
Copy link
Member

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
Copy link
Member

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.

@d180cf
Copy link

d180cf commented Oct 20, 2015

@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
Copy link

eggers commented Aug 24, 2017

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

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jun 26, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone Jun 26, 2018
@mhegazy mhegazy removed the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Jun 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants