microsoft / microsoft/TypeScript
Inferring function parameters from the return type
Nessuno ha ancora preso questa issue.
- 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
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
- What do you want to use this for?
Helpful for strict typings and intellisense.
- What shortcomings exist with current approaches?
None give a strict enough type.
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- 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