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

Ability to replace return type of function type #39594

Open
5 tasks done
justinfagnani opened this issue Jul 14, 2020 · 3 comments
Open
5 tasks done

Ability to replace return type of function type #39594

justinfagnani opened this issue Jul 14, 2020 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@justinfagnani
Copy link

Search Terms

  • replace return type
  • parameters generic

Suggestion

I would like to be able to replace just the return type of a function with a utility type, like WithReturn<F, R>.

I'm not 100% sure this is needed, but it seems like using (...args: Parameters<F>) => R is not sufficient because any generics in F are lost.

Use Cases

My use case is a utility function that captures the arguments to a function to allow it to be called later.

Examples

export const defer = <F extends (...args: any) => any>(f: F) => ((
  ...args: Parameters<F>
) => ({
  function: f,
  args,
})) /* as any as F */;

const map = <T, R>(array: Array<T>, f: (i: T) => R) => array.map(f);
const deferredMap = defer(map);

let r = deferredMap([1], (i: number) => 2); // error now: Type 'unknown' is not assignable to type 'number'.(2345)
r.args; // error if defer returns F

Right now there's an error because the generics to map() are lost when using Parameters.

Playground: https://www.typescriptlang.org/play/?ssl=13&ssc=1&pln=1&pc=1#code/KYDwDg9gTgLgBAYwgOwM7wCbAGbCnAXjgB4AxOUGYZDVOACgDpmBDKAc1QC44XkBPAJSEAfLwEj62HqWEEx9egCg4cZozaceABTYsAtsCpRUZEUrkKA3irjYArsgQwAlih7YANLc2pvAX0FhAHoAKl46Pn4IuHJQ4IBuJSUkNHh9FjBCEgAVTzgAJUk2KBZ+HgBBKFL+YhyRfOkGFx4cy0L2krLGDLApQSTU9DgsXGrgDABZTOzRvHpegeSAGyM4fCI58anM+gBtAEYAXXz6Frhke30AIzx2gCYlqA0OVCSgA

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jul 15, 2020
@awerlogus
Copy link

Very useful feature. This is the only way to implement flow function with unlimited parameters count, for example. We just need to recursively compute return type of the functions chain passed (this can be done using #38182) and then replace return type of the first function with it. There're very much cases of functions wrapping other functions and changing its return types. @RyanCavanaugh, You really should pay attention to this proposal.

@TylorS
Copy link

TylorS commented Aug 22, 2020

This would indeed be a very useful feature! I think it might be incredibly useful to have a syntax for mapping over the entirety of a function type including type parameters and return type, similar to a mapping over object/tuple types. Does that sound useful to others in the thread?

@awerlogus
Copy link

@TylorS We need to look at the code examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants