microsoft / microsoft/TypeScript

Simple Comparison of Generics not allowed?

Offen
#38,220 1 Kommentar 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Experience Enhancement Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TypeScript Version: 3.8

Search Terms:
generic comparison
compare generic strings

Code

function test<A, B>(one: A, two: B): A {
  if (one === two) {
    throw new Error('no');
  }
  return one;
}

const obj = {};

test(obj, obj);

Expected behavior:

While the generics are generally indicating that one and two are independent values, they can still potentially be the same underlying value so they should be able to at least be checked for equality without casting them first, especially since there is no error produced when calling the function with identical values in the first place.

At the very least the error message should be more specific since the assumption it is providing is not correct in since the condition returns true :-P

( This is clearly a simplified version of where I ran into this since there'd be no reason to use generics in this specific case :-) )

Actual behavior:

Shown in example

Playground Link: Playground

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 dem TypeScript 3.8 Playground-Link und der generischen test<A, B>-Reproduktion im Issue. Untersuche die Diagnose des Type-Checkers beim Vergleich von one === two, wenn A und B unabhängige Typparameter sind. Als abgeschlossen gilt die Aufgabe, wenn das Vergleichsverhalten oder dessen Diagnose der angegebenen Erwartung entspricht und das gezeigte Beispiel abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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