Skip to content

Type inference breaks after the simple assignment operation. #26673

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
dizys opened this issue Aug 25, 2018 · 2 comments
Closed

Type inference breaks after the simple assignment operation. #26673

dizys opened this issue Aug 25, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@dizys
Copy link

dizys commented Aug 25, 2018

TypeScript Version: 3.1.0-dev.20180824

Search Terms: Type Inference, Union Type, Assignment Operation

Code

// Tested by running `tsc` on the command-line
// strictNullChecks: on

interface BarA {
  label: true;
  yoha?(): string;
}

interface BarB {
  label: false;
  yoha?(): void;
}

class Foo {
  bar!: BarA | BarB;

  pia(): void {
    if (this.bar.yoha) {
      this.bar.yoha(); // passed compiling
    }

    if (this.bar.yoha) {
      if (this.bar.label) {
        let label = this.bar.yoha(); // passed
      } else {
        this.bar.yoha(); // passed
      }

      this.bar.yoha(); // passed
    }

    let bar = this.bar;

    if (bar.yoha) {
      bar.yoha(); // passed
    }

    if (bar.yoha) {
      if (bar.label) {
        let label = bar.yoha(); // error while compiling: Object is possibly 'undefined'.
      } else {
        bar.yoha(); // error: Cannot invoke an object which is possibly 'undefined'.
      }

      bar.yoha(); // error: Cannot invoke an object which is possibly 'undefined'.
    }
  }
}

Expected behavior:

bar should behave the same as this.bar since they are indentical and passed by the simplest assignment operation.

Actual behavior:

Type inference for bar seems to break down after being initialized as the indentical of this.bar, shown as the demonstration code.

Playground Link: http://www.typescriptlang.org/play/#src=interface%20BarA%20%7B%0D%0A%20%20label%3A%20true%3B%0D%0A%20%20yoha%3F()%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20BarB%20%7B%0D%0A%20%20label%3A%20false%3B%0D%0A%20%20yoha%3F()%3A%20void%3B%0D%0A%7D%0D%0A%0D%0Aclass%20Foo%20%7B%0D%0A%20%20bar!%3A%20BarA%20%7C%20BarB%3B%0D%0A%0D%0A%20%20pia()%3A%20void%20%7B%0D%0A%20%20%20%20if%20(this.bar.yoha)%20%7B%0D%0A%20%20%20%20%20%20this.bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20if%20(this.bar.yoha)%20%7B%0D%0A%20%20%20%20%20%20if%20(this.bar.label)%20%7B%0D%0A%20%20%20%20%20%20%20%20let%20label%20%3D%20this.bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%20%20%7D%20else%20%7B%0D%0A%20%20%20%20%20%20%20%20this.bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%20%20this.bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20let%20bar%20%3D%20this.bar%3B%0D%0A%0D%0A%20%20%20%20if%20(bar.yoha)%20%7B%0D%0A%20%20%20%20%20%20bar.yoha()%3B%20%2F%2F%20worked%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20if%20(bar.yoha)%20%7B%0D%0A%20%20%20%20%20%20if%20(bar.label)%20%7B%0D%0A%20%20%20%20%20%20%20%20let%20label%20%3D%20bar.yoha()%3B%20%2F%2F%20failed%0D%0A%20%20%20%20%20%20%7D%20else%20%7B%0D%0A%20%20%20%20%20%20%20%20bar.yoha()%3B%20%2F%2F%20failed%0D%0A%20%20%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%20%20bar.yoha()%3B%20%2F%2F%20failed%0D%0A%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%7D

Related Issues: not yet

@ghost
Copy link

ghost commented Aug 26, 2018

Duplicate of #19955

@ghost ghost marked this as a duplicate of #19955 Aug 26, 2018
@ghost ghost added the Duplicate An existing issue was already created label Aug 26, 2018
@acontreras89
Copy link

acontreras89 commented May 6, 2024

This was marked as a duplicate of #19955 which was actually related to a particular case of using if statements in loops. That was fixed, yet this problem remails.

#27706 seems to be the same problem but it is treated as specific case when the argument/variable is initially of type unknown.

So, is there an open issue for this?

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

3 participants