Microsoft/TypeScript
Destructuring assignment with computed property name does not help narrow types as intended.
オープン
#62,401 opened on 2025/09/05
Domain: check: Control FlowHelp WantedPossible Improvement
Repository metrics
- Stars
- (108,860 個のスター)
- PR merge metrics
- (平均マージ 6d 17h) (30d で 9 merged PRs)
説明
🔎 Search Terms
"destructuring assignment", "computed property name", "type narrowing"
🕗 Version & Regression Information
Happen to all existing version in typescript playground.
⏯ Playground Link
💻 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