microsoft / microsoft/TypeScript

Unary minus on 'any' should infer type to 'number | bigint', not just 'number'

Offen
#60,914 3 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
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

https://www.typescriptlang.org/play/?target=99&ts=5.7.2#code/MYewdgzgLgBARjAvDAtACgIxgJQG4BQA9ITKTAHoD8+okscATEqnAcWRdTeNDGM+gx4iJMlW50+TZCjBtRpcRN4BDAFwwVYAJ7NWIjuNq8AHgJXzDXY7FoA3Zmjsat27EgB8qO5bHUbMABOzPZocMLsfsq24HbSME4aYACuALZwAKbBAD7wAJYA5nlgUO6IXig+BlEBgfH2DGERCpz4QA

💻 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

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 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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.