microsoft / microsoft/TypeScript
Additive inverse of number literals
Dieses Issue hat noch niemand übernommen.
- 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
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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