Microsoft/TypeScript

Aliased optional chain calls don't narrow like their inline equivalents

Open

#59,145 创建于 2024年7月5日

在 GitHub 查看
 (0 评论) (2 反应) (0 负责人)TypeScript (6,726 fork)batch import
Domain: check: Control FlowHelp WantedPossible Improvement

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

🔎 Search Terms

aliased conditions control flow analysis cfa optional chain call

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240704#code/GYVwdgxgLglg9mABAJwKYGc4BsBuqCMAFDACYBci6UyMYA5ogD6LgmrC2okCUiA3gChEiCAiqIY6AEKssXRAF4JJAPwA6WhCwg26QgCIA+gCNZXfdwDcQicETFpZnvxvDRYcVWQUvtBktJrYQBfAVCBUEhYBBQMbDwAJmJySmo-JhYwNg4wLl5BYRg7ZPVNbV0DEycLfNcRMShU7yb0gJIgxFDgoA

💻 Code

function resolve1(id: string | undefined) {
  const isBundled = id?.includes("_bundled");
  if (isBundled) {
    const str: string = id; // error
  }
}

function resolve2(id: string | undefined) {
  if (id?.includes("_bundled")) {
    const str: string = id; // ok
  }
}

🙁 Actual behavior

resolve1 errors within the if block

🙂 Expected behavior

I'd expect it to be OK. Just like resolve2 is OK with it here - the condition is exactly the same and TS supports Control Flow Analysis of Aliased Conditions and Discriminant since 4.4

Additional information about the issue

No response

贡献者指南