Sitelet https://web.archive.org/web/20250512130326/https://github.com/microsoft/TypeScript/issues/40745
Skip to content

Boolean value should be infered boolean instead of false #40745

Closed
@Xstoudi

Description

@Xstoudi

TypeScript Version: 3.3.3 to Nightly

Search Terms:

  • Boolean
  • Iteration
  • Inference

Code

let value = false;

['a', 'b', 'c'].forEach(str => {
  if (str === 'b') {
    value = true;
  }
})

console.log(value)

Expected behavior: Type inspection (mouse on variable) should say that value type is a boolean as the variable can clearly take both true and false values.

Actual behavior: It says value can only be false, even after conditionnaly set value to true.

Playground Link: https://www.typescriptlang.org/play?ts=4.0.2#code/DYUwLgBAbghsCuIIF4IDM4GcQG4BQeA2gOQzEA0ExARhVQMbEC6AdGgPYBOAojPQBYAKTGE4oAfBADeeCBACWaCMNEpkqGsQCU02XOhxEKCKMT45AXzwWtBeuwB2mdqBbB2Ac0GwEIW0A

Related Issues: Not found, let me know if there is already one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions