Skip to content

False negatives and false positives for "Object is possibly 'null'" warning in typescript@next with "complex" if #10198

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
donaldpipowitch opened this issue Aug 8, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@donaldpipowitch
Copy link
Contributor

donaldpipowitch commented Aug 8, 2016

TypeScript Version: 2.1.0-dev.20160807

Code

class Nested {
  data: string;
}

class Foo {
  bar: Nested | null = null;
  baz: number = 1;

  get data(): null | string {
    if (this.bar === null || this.baz !== 1) {
      return null;
    } else {
      return this.bar.data;
             // ^~~~~ Object is possibly 'null'. (BUT IT CAN'T BE NULL.)
    }
  }
}

Expected behavior:

No error.

Actual behavior:

An error.

Details:

This failure must be new. I think I installed typescript@next last week and had no error. I installed it today and it throws an error now.

If a you remove the second if-branch (this.baz !== 1) it works.

This also throws:

  get data(): null | string {
    if (this.bar === null) {
      return null;
    } else if (this.baz !== 1) {
      return null;
    } else {
      return this.bar.data;
             // ^~~~~ Object is possibly 'null'. (BUT IT CAN'T BE NULL.)
    }
  }

This works:

  get data(): null | string {
    if (this.baz !== 1) {
      return null;
    } else if (this.bar === null) {
      return null;
    } else  {
      return this.bar.data;
    }
  }

(!) On the other hand: This works, but could be wrong so it should throw an error!

  get data(): null | string {
    if (this.baz !== 1 || this.bar === null) {
      return null;
    } else {
      return this.bar.data;
    }
  }
@donaldpipowitch donaldpipowitch changed the title Incorrect "Object is possibly 'null'" warning in typescript@next with "complex" if False negatives and false positives for "Object is possibly 'null'" warning in typescript@next with "complex" if Aug 8, 2016
@yortus
Copy link
Contributor

yortus commented Aug 8, 2016

This also works:

  get data(): null | string {
    let x = this.bar; // x is Nested | null
    if (x === null || this.baz !== 1) {
      return null;
    } else {
      return x.data; // OK
    }
  }

@ahejlsberg
Copy link
Member

Duplicate of #10169, fix available in #10188.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Aug 8, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants