Skip to content

Destructuring assignment with var bypasses "variable is used before being assigned" check (2454)  #50551

@selimb

Description

@selimb

Bug Report

🔎 Search Terms

  • var destructuring used before being assigned
  • 2454

🕗 Version & Regression Information

  • This changed between versions 3.3.3 and 3.5.1
  • This is still present for all versions >= 3.5.1 on TS playground, including Nightly (4.9.0-dev.20220830).

⏯ Playground Link

Playground link with relevant code

💻 Code

declare function getUser(): {user: string};
try {
  var { user } = getUser();
} catch {
  console.error("error");
}
console.info(user.toUpperCase());

🙁 Actual behavior

Code above compiles without errors, but crashes at runtime if getUser throws since user will be undefined -- see TS playground for easy reproduction.

⚠️ Additionally, the behaviour is the same for array destructuring.

🙂 Expected behavior

I'd expect the following error on the last line:

Variable 'user' is used before being assigned. (2454)

As shown in the playground, this error is correctly raised if the variable (r) is initialized using "regular assigment" instead of destructuring assignment (user).

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions