Microsoft/TypeScript

Destructuring assignment with computed property name does not help narrow types as intended.

Open

#62,401 创建于 2025年9月5日

在 GitHub 查看
 (4 评论) (0 反应) (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

"destructuring assignment", "computed property name", "type narrowing"

🕗 Version & Regression Information

Happen to all existing version in typescript playground.

⏯ Playground Link

https://tsplay.dev/w1eElw

💻 Code

declare let obj: Record<string, string>

declare let cond: boolean;
declare let key: string;
let result: string | null = null;

if (cond) {
    ({ [key]: result } = obj);
    result;
//  ^?
}

if (cond) {
    ({ 'key': result } = obj);
    result;
//  ^?
}

🙁 Actual behavior

Type narrowing did not happen to the assignee variable result after the assignment ({ [key]: result } = obj);.

🙂 Expected behavior

Expecting the assignment ({ [key]: result } = obj); will narrow the assignee variable result to non-null.

Additional information about the issue

No response

贡献者指南