microsoft / microsoft/TypeScript
Unary minus on 'any' should infer type to 'number | bigint', not just 'number'
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dal TypeScript Playground collegato e riproduci i tipi dedotti per il meno unario su any, number, bigint e number | bigint. Traccia la gestione da parte del compilatore dell'inferenza dei tipi del meno unario e i relativi test; il lavoro è completo quando il meno unario su any viene dedotto come number | bigint, mentre il comportamento esistente per number e bigint rimane corretto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 38/100