microsoft / microsoft/TypeScript

Suggestion: allow `infer T` in type parameter constraint of `type` statement

Offen
#39,736 2 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

suggestion infer extends type parameter constraint

Suggestion

Allow infer T in the right of extends of a type statement.

Examples

This suggestion allows following code:

type ReturnType<T extends (...args: any) => infer R> = R;

Which behaves similarly as:

// this code is taken from lib.es5.d.ts
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

Use Cases

As demonstrated in the example, we sometimes use conditional types with the sole purpose of use of infer T.
We have to repeat some piece of code if we want to constraint what types can be passed to ReturnType.
So this suggestion provides an abbreviated syntax for this use case.

Open questions:

  • Should union distribution occur for the new syntax?
    • My opinion is yes to align with existing conditional types
    • hmm, but then just replacing any with infer _ would change the behavior...
  • Should function type parameters also allow infer T?
    • No strong opinion

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.

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

Es werden keine Quelldateien, Tests oder Einstiegspunkte genannt. Beginne mit der Prüfung der vorgeschlagenen infer T-Syntax und ihres Vergleichs bedingter Typen; kläre anschließend die offenen Fragen zur Verteilung von Unions und zu Typparametern von Funktionen. Als erledigt gilt die Aufgabe, wenn Syntax und Semantik ausreichend genau spezifiziert sind, um sie zu implementieren und zu testen.

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
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

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