microsoft / microsoft/TypeScript
Nullish coalescing should always include the type of the right operand
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
$ ./node_modules/.bin/tsc --version
Version 3.7.2
Search Terms:
Code
A toy example would be something like this.
let foo: string = "";
foo = "bar" ?? 123;
This one is obviously fine since "bar" is always truthy.
However, this becomes a little bit problematic when you consider the idiom of having Record objects and checking their truthiness before using them.
const elts = ["foo", "bar", "spam", "spam", "foo", "eggs"];
const counts: Record<string, number>;
for (const elt of elts) {
// This really **should** raise an error.
counts[elt] = counts[elt] ?? "zero";
counts[elt] += 1;
}
Expected behavior:
An error should be raised.
Actual behavior:
Curiously, an error is not raised in strict mode but is raised in un-strict mode.
$ ./node_modules/.bin/tsc ./foo.ts
foo.ts:5:3 - error TS2322: Type 'number | "zero"' is not assignable to type 'number'.
Type '"zero"' is not assignable to type 'number'.
5 counts[elt] = counts[elt] ?? "zero";
~~~~~~~~~~~
Found 1 error.
$ ./node_modules/.bin/tsc --strict ./foo.ts
# No error, exits 0 and emits JS.
Playground Link:
Playground Link
Toggling the strictNullChecks config option will show the issue.
Related Issues:
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
TypeScript 3.7.2 を使用し、コンパイラーのデフォルトコマンドと --strict の両方で例を再現してから、nullish coalescing 代入に対する診断を比較します。報告された式に右オペランドの型が一貫して含まれ、指定された構成で期待されるエラーが発生すれば、作業は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100