Skip to content

Property with getter: This condition will always return 'false'Β #46771

Closed

Description

Bug Report

πŸ”Ž Search Terms

  • getter condition

πŸ•— Version & Regression Information

  • This seems like intended behavior (it is sometimes, but not in this case)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class Example {
  private get _foo() {
    return Math.random() < 0.5 ? "a" : "b";
  }

  bar() {
    if (this._foo === "a") {
      console.log("got 'a'");
      // this error should not occur:
      if (this._foo === "b") {
        console.log("lucky case, got 'b' too");
      }
    }
  }
}

class Span {
  get end() { return this.start + this.length }
  constructor(public start: number, public length: number) {}
}

const span = new Span(4, 2);

if (span.end == 6) {
  span.length += 1;
  // same here:
  if (span.end == 7) {
    console.log("worked");
  }
}

πŸ™ Actual behavior

An error occured because TypeScript thinks this._foo/span.end doesn't change until that if-check, but in these examples it does.

πŸ™‚ Expected behavior

There shouldn't be such checks with getters.

Activity

  1. MartinJohns commented on Nov 11, 2021

    @MartinJohns
    Contributor

    Another duplicate of #9998.

  2. fatcerberus commented on Nov 11, 2021

    @fatcerberus

    For the record: TypeScript makes no distinction between getters and plain properties at the type level.

  3. typescript-bot commented on Nov 13, 2021

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions