microsoft / microsoft/TypeScript
typeof return
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Suggestion
When return type of a function is known, introduce a special keyword which gives access to that return type.
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Introduce a mechanism to use the return type of the current function, when it is known in advance.
📃 Motivating Example
Consider the example below where return type string of function type F is known in advance, but the type F might be unavailable, at least easily. In that case, the return type of the F is known, but there is no alias to make use of it.
There could be a magic alias, say typeof return, which holds the return type of the current function, if that type is defined in advance.
type F = () => string;
const implementF = (implementation: F) => {};
implementF(() => {
const result: typeof return = 'abc'; // string
return result;
});
Here return is a reserved keyword in JavaScript, so there should be no identifier named "return".
Basically typeof return would return the type (if known), that would be expected, if there was a ReturnStatement at the place where typeof return was written.
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 esaminando i TypeScript Design Goals collegati nell’issue, quindi analizza come dovrebbe comportarsi il ritorno typeof proposto per le espressioni di funzione tipizzate contestualmente. Il lavoro sarebbe completato quando fosse definito un design risolto per la sintassi e la semantica, incluso quando il tipo di ritorno è noto; non vengono indicati file di implementazione né test.
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