microsoft / microsoft/TypeScript

Spurious "referenced directly or indirectly in its own initializer" when combined with narrowing, asserts function, and loop

オープン
#53,955 コメント 3 件 リアクション 2 件 担当者 0 名 GitHub で見る

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

Experience Enhancement Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

referenced directly or indirectly asserts

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground Link

💻 Code
declare const myRequire: ((p: string) => {} | undefined) | undefined;

declare function assertIsDefined<T>(value: T): asserts value is NonNullable<T>

function fn1() {
    if (!myRequire) {
        return;
    }

    for (const p of ["a", "b"]) {
        // Error?
        const result = myRequire("something");
        assertIsDefined(result);
    }
}
🙁 Actual behavior

result is claimed to be referenced directly or indirectly in its initializer.

Modifying the code in various ways makes the error go away.

🙂 Expected behavior

No error; there's no relationship between the initializer and the result.

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

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

はじめの一歩

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

調査の方向性

リンクされている TypeScript Playground から始め、提供されたコードサンプルを実行して診断を再現します。コンパイラーが narrowing、asserts functions、および initializer の周辺のループをどのように処理しているかを調査します。サンプルが result が自身の initializer 内で参照されていると報告しなくなり、期待される narrowing の動作が維持されれば完了です。

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

評価

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

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

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