microsoft / microsoft/TypeScript

Object is possibly undefined after undefined check on readonly property

オープン
#39,340 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Experience Enhancement Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 3.7.4, 3.9.2

Search Terms:
Object is possibly undefined
undefined readonly callback
Code

class Test {
  private readonly testProperty?: { value: number };
  public constructor(testProperty: { value: number }) {
    this.testProperty = testProperty;
  }

  public method() {
    if (this.testProperty) {
      Promise.resolve().then(() => this.testProperty.value); //Object is possibly undefined
    }
  }
}

Expected behavior:
testProperty should not be resolved as possibly undefined as this is not possible in this scenario as the property has already been checked and it is marked as readonly.

Actual behavior:
testProperty is possibly undefined, requiring another undefined check.

Playground Link:
https://www.typescriptlang.org/play/#code/MYGwhgzhAEAqCmEAu0DeAoa0AOAnAlgG5hLzS7xgAmA9gHYgCe0pyACrjdvLkowPwAuNNGIgArvGF1xAWwBGPaAF8A3Jhzj5IfMGjB6yXOOBIauABSskHLjz7DUosBKnQZCpcoCUaDViQAC3wIADprW25eZgBeFkQbTii+dSxldA1sLR09WXggmioLXwwsLHwAM2grYLCIpPtGEv8y21kQ+FCKCBoQQnhi8MD4Ogti6BiAPhZa8ITIxtCxSW9VaAB6dYB5eQAreFNoEJwaKHxtZnE6KngK-Dp4Khb0tPRlIA

Related Issues:

Similar issues but these i understand why TypeScript would report an error, in my case the property is readonly so it seems like a bug to me.
https://github.com/microsoft/TypeScript/issues/38053
https://github.com/microsoft/TypeScript/issues/11498

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされている Playground の再現例から始め、関連する issue #38053 と #11498 に記載された挙動を比較してください。Promise コールバック内の readonly optional property に対する型チェックを追跡してください。完了の条件は、提示された例で「possibly undefined」が報告されなくなるか、期待される挙動が別の形で解決されることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。