Microsoft/TypeScript

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

Open

#62 401 ouverte le 5 sept. 2025

Voir sur GitHub
 (4 commentaires) (0 réactions) (0 assignés)TypeScript (6 726 forks)batch import
Domain: check: Control FlowHelp WantedPossible Improvement

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

🔎 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

Guide contributeur