Skip to content

try-finally causes "variable is used before being assigned" #32836

Description

TypeScript Version: 3.5.3

Search Terms:

  • variable is used before being assigned
  • try finally variable

Code

function bug() {
  let close: () => void | undefined;
  if (process.env.NODE_ENV === 'development') {
    close = () => {}
  }
  try {
    // ...
  } finally {
    if (close) { // Variable 'close' is used before being assigned.ts(2454)
      close()
    }
  }
}

Expected behavior:

Should compile without errors

Actual behavior:

Does not compile: Variable 'close' is used before being assigned.ts(2454)

Playground Link:

http://www.typescriptlang.org/play/index.html#code/GYVwdgxgLglg9mABAIxAcwBQEpEG8BQiiANgKZSITFwDOpAXItogLwB8iAbnDACYDchRDGBMADgCc4EUjRoA6UmE7yAcgHkAIgFEA+ttUA1VixaIA5L1KdS1MQFslUczgJEiVWqVZMc7PAC+QkFEUBIAnnhCRAD0MYjyicGIwDBgAIbExJFu7iJMnnSu0e6FpNglIYhBAUA

Related Issues:

#29684
#12205
#12355

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions