microsoft / microsoft/TypeScript

Support Implicit Generics Inference

Offen
#38,591 3 Kommentare 1 Reaktion 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

Search Terms

implicit generics, broader type inference, better type inference

Suggestion

I'd like TypeScript to know when two types are unknown, but equal.
That is, say I've function equals():

type T = { type: 'a', p1: boolean } | { type: 'b', p2: string };

function equals(a: T, b: T) {
  // Currently, we have to write:
  if (a.type === 'a' && b.type === 'a') { return a.p1 === b.p1;  }
  if (a.type === 'b' && b.type === 'b') { return a.p2 === b.p2; }
  return false;

  // But I'd like to be able to write:
  if (a.type !== b.type) { return false; }
  return a.type === 'a' ? a.p1 === b.p1 : a.p2 === b.p2;
}

Use Cases

I can't think about other use cases except comparisons (if you know let me know), but I think they're common enough. This will also make them somewhat faster (no need to check both's type).

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 Repository-Dateien, Tests oder Einstiegspunkte genannt. Beginne damit, das equals-Beispiel in TypeScript nachzustellen, und bestimme das erwartete Narrowing-Verhalten, wenn die Diskriminanten gleich sind. Als erledigt gilt die Aufgabe, wenn die angeforderte implizite Inferenz funktioniert, ohne das bestehende Type-Checking-Verhalten zu beeinträchtigen, und Tests das Beispiel abdecken.

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
35/100

Neue Issues direkt in Ihr Postfach

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