microsoft / microsoft/TypeScript

Suggestion: error when using relational operators on types whose valueOf() method returns 'never' or 'void'

Offen
#52,773 4 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Suggestion

🔍 Search Terms

valueof never

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    • (This would be a breaking change inasmuch as it will generate additional errors.)
  • 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.
    • (I hope so!)

⭐ Suggestion

At runtime, each operand of a relational operator is converted to a primitive value if needed by calling its valueOf() method (among other things). A type can be made effectively incomparable by giving it a valueOf method that throws an exception.

Ideally, tsc would check the valueOf signature of each operand, and produce a type error for any relational expression that implicitly invokes a valueOf whose declared return type is never or void.

📃 Motivating Example

The following code unconditionally throws at runtime and passes type-checking.

class C {
    valueOf() {
        throw new Error("Please don't call me again!")
    }
}
const a = new C();
const b = new C();
if (a < b) { // <- No error
    console.log("I am unreachable");
}

Playground link

💻 Use Cases

TC39 Temporal PlainDate and Instant types throw an exception when compared this way (for reasonable reasons). It's easy to accidentally write code that naively compares these types using an operator instead of the appropriate compare method. It would be ideal if this was caught statically.

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

Beginnen Sie mit dem motivierenden Beispiel für einen relationalen Ausdruck im Issue und dem verlinkten TypeScript Playground. Untersuchen Sie, wie der Typprüfer relationale Operatoren behandelt und ob er die Rückgabetypen von valueOf() untersucht. Als abgeschlossen gilt die Aufgabe, wenn Ausdrücke, die implizit valueOf() aufrufen und never oder void zurückgeben, einen Typfehler erhalten, ohne das ausgegebene JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, 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.