microsoft / microsoft/TypeScript
Unary minus on 'any' should infer type to 'number | bigint', not just 'number'
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
"unary minus", "negate bigint"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about bigint
⏯ Playground Link
💻 Code
const b = -(1n);
// ^?
const b2 = -b;
// ^?
const n = -(1);
// ^?
const n2 = -n;
// ^?
const a: any = b;
// ^?
const x = -a;
// ^?
const conv = (v: any) => -v;
// ^?
const r = conv(b);
// ^?
const conv2 = (v: number | bigint) => -v;
// ^?
const r2 = conv2(b);
// ^?
🙁 Actual behavior
Applying an unary minus on an any variable infers the type to number (as can be seen in the "hat-question mark" comments or the .D.TS tab for x, conv, and r). The inferred type for the constants x and r is thus wrong (they actually contain a BigInt value).
Unary minus on a number correctly infers number, and (more importantly) unary minus on a bigint correctly infers bigint. Same for unary minus on a number | bigint which infers number | bigint.
🙂 Expected behavior
Applying an unary minus on an any variable should infer the type to number | bigint.
ECMAScript specifies (https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-unary-minus-operator) unary minus as doing a ToNumeric (which returns either a Number of a BigInt) and then applying the appropriate unaryMinus abstract operation depending on the type of the value. The result is thus either a Number or a BigInt.
(the unary plus operator however converts to Number values only)
Additional information about the issue
No response
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 dem verlinkten TypeScript Playground und reproduziere die abgeleiteten Typen für unäres Minus bei any, number, bigint und number | bigint. Verfolge die Behandlung der Typinferenz für unäres Minus durch den Compiler und die zugehörigen Tests; abgeschlossen ist die Aufgabe, wenn unäres Minus mit any als number | bigint abgeleitet wird, während das bestehende Verhalten für number und bigint korrekt bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 38/100