Skip to content

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

@hkleungai

Description

@hkleungai

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: check: Control FlowThe issue relates to control flow analysisHelp WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions