microsoft / microsoft/TypeScript
Unary minus on 'any' should infer type to 'number | bigint', not just 'number'
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンク先の TypeScript Playground から始め、any、number、bigint、number | bigint に対する単項マイナスの推論型を再現してください。コンパイラによる単項マイナスの型推論の扱いと、それに関連するテストを追跡してください。完了の条件は、any に対する単項マイナスが number | bigint と推論され、既存の number および bigint の動作が正しいままであることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 38/100