microsoft / microsoft/TypeScript

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

未关闭
#53,955 3 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Experience Enhancement Suggestion
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 TypeScript Playground 开始,运行提供的代码示例以复现该诊断。调查编译器如何处理 narrowing、asserts functions 以及 initializer 周围的循环。当示例不再报告 result 在其自身的 initializer 中被引用,同时预期的 narrowing 行为仍保持不变时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。