Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
blaumeise20 opened this issue Nov 11, 2021 · 3 comments
Closed

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

blaumeise20 opened this issue Nov 11, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@blaumeise20
Copy link
Contributor

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.

@MartinJohns
Copy link
Contributor

Another duplicate of #9998.

@fatcerberus
Copy link

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

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 11, 2021
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants