microsoft / microsoft/TypeScript

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

Aperta
#52,773 4 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’esempio motivante di un’espressione relazionale nell’issue e nel TypeScript Playground collegato. Analizza come il verificatore dei tipi gestisce gli operatori relazionali e se esamina i tipi restituiti da valueOf(). Il lavoro è completo quando le espressioni che chiamano implicitamente valueOf() e restituiscono never o void ricevono un errore di tipo senza modificare il JavaScript emesso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.