microsoft / microsoft/TypeScript

Ability to replace return type of function type

Aperta
#39,594 5 commenti 15 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il comportamento di perdita dei generici nel TypeScript Playground collegato, concentrandoti su Parameters e sul comportamento proposto di WithReturn<F, R>. Il lavoro è completato quando un’utility può preservare i parametri generici di un tipo di funzione sostituendo solo il tipo restituito, senza modificare l’output a runtime di JavaScript.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.