microsoft / microsoft/TypeScript

Additive inverse of number literals

Offen
#30,914 1 Kommentar 5 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

Using unary minus on variable that is of number-literal type. Negative number literals.

Suggestion

This is a specific case of #26382. My proposal is simply that if we apply unary minus on an expression whose type is a union of number literal types, the result should be a union of their additive inverses:

declare var a: 1 | 2 | 3;
const b = -a; // should be -1 | -2 | -3

Furthermore we could add a unary minus type operator:

-(42) === -42
-(A | B) === -A | -B
-number === number
-(not number) // error

Both should be very easy and uncontroversial to implement.

Use Cases

function compare(a: C, b: C): -1 | 0 | 1;

function reverseCompare(a: C, b: C): -1 | 0 | 1
{
  return -compare(a, b); // shouldn't throw an error
}

The type operator would be just for sake of completeness

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

Beginne mit der Prüfung des zugehörigen Vorschlags in Issue #26382 und des Typprüfungsverhaltens für das unäre Minus bei Unions von Zahlenliteralen. Erledigt ist die Aufgabe, wenn die Beispiele -1 | -2 | -3 inferieren und reverseCompare akzeptiert wird, während ungültige Operanden, die keine Zahlen sind, weiterhin Fehler verursachen; im Issue werden keine Quelldateien oder Tests genannt.

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.