microsoft / microsoft/TypeScript

Ability to replace return type of function type

Open
#39,594 5 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the generic-loss behavior in the linked TypeScript Playground, focusing on Parameters and the proposed WithReturn<F, R> behavior. Done means a utility can preserve the generic parameters of a function type while replacing only its return type, without changing JavaScript runtime output.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.