microsoft / microsoft/TypeScript

Inferring function parameters from the return type

Aperta
#56,311 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

"inferring parameters types", "infer input from output types"

✅ Viability Checklist
⭐ Suggestion

I would like to infer the types of the parameters of a function based on the return type. I have tried a few patterns and I don't think this is currently possible.

📃 Motivating Example
const inferReturnValuesAsKeys = <
  T extends (arg: keyof ReturnType<T> & string) => Record<string, number>,
>(
  t: T,
) => t;

/**
 * @todo arg should be typed as 'a' instead of 'string'
 */
inferReturnValuesAsKeys((arg) => ({ a: 5 }));
// ^?
💻 Use Cases
  1. What do you want to use this for?

Helpful for strict typings and intellisense.

  1. What shortcomings exist with current approaches?

None give a strict enough type.

  1. What workarounds are you using in the meantime?

I don't have the type safety that I want.

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 con l'esempio motivante in TypeScript e rivedi i Design Goals collegati e il comportamento di inferenza esistente. Determina se il compilatore può inferire il parametro del callback come chiave dell'oggetto restituito, preservando l'output JavaScript e il comportamento esistente del controllo dei tipi; il lavoro è completato quando l'esempio ha il tipo richiesto e test adeguati lo coprono.

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
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.