microsoft / microsoft/TypeScript
Type inference/narrowing lost after assignment
オープン
まだ誰も着手していません。
In Discussion
Suggestion
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
TypeScript Version: 3.1
Search Terms: type inference, type guard, narrowing, lost, assignment
Code
let a: unknown = 'x';
if (typeof a === 'string') {
// a inferred as `string`
a = a.substr(0, 5); // (method) String.substr(from: number, length?: number): string
// a inferred as `unknown`
a.length; // Failure: Object is of type 'unknown'.
}
Expected behavior: This should compile without an error.
Actual behavior: Line 7 fails with: Object is of type 'unknown'.
Related Issues: #18840, #19955, #26673
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
TypeScript Playground で最小限の例を再現し、関連する issue #18840、#19955、#26673 と動作を比較してください。typeof ガード内の代入によって string への絞り込みが維持され、最終的な a.length 式がエラーなしでコンパイルされれば、作業は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 48/100