microsoft / microsoft/TypeScript

typeof return

Offen
#53,900 5 Kommentare 5 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Prüfung der im Issue verlinkten TypeScript Design Goals und analysiere anschließend, wie sich der vorgeschlagene typeof-Rückgabewert für kontextuell typisierte Funktionsausdrücke verhalten sollte. Als abgeschlossen gilt die Aufgabe, wenn ein geklärtes Design für Syntax und Semantik vorliegt, einschließlich der Fälle, in denen der Rückgabetyp bekannt ist; es werden keine Implementierungsdateien oder Tests genannt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.