Skip to content

False positive "Object is possibly 'null'" error #1056

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

Open
vladimir-tikhonov opened this issue Jan 12, 2020 · 3 comments
Open

False positive "Object is possibly 'null'" error #1056

vladimir-tikhonov opened this issue Jan 12, 2020 · 3 comments

Comments

@vladimir-tikhonov
Copy link
Contributor

vladimir-tikhonov commented Jan 12, 2020

Note: it appears only in the last nightly version, so it's probably caused by one of the pull requests merged yesterday.

webassembly studio: https://webassembly.studio/?f=ih6p4z4fub

The following code is now producing an invalid error during compilation:

class Container {
  public another: Another|null = null;
}

class Another {
  public prop: i32 = 0;
}

export function doStuff( container: Container ): void {
  if ( container.another !== null && container.another.prop > 1 ) { // TS2531: Object is possibly 'null'
    container.another.prop = 2;
  }
}

This was not a case previously and not a case in the current version of typescript.

@dcodeIO
Copy link
Member

dcodeIO commented Jan 12, 2020

This is a side-effect of #1046 where I enabled null checking of property accesses after fixing several flow problems preventing it.

Underlying problem this uncovers is that the compiler doesn't currently attempt to null-check repeated accesses to class fields, which can be tricky since other code can theoretically unset the field as a side-effect, and due to static typing we must be 100% sure about that.

Until there's a solution, caching in a local should work:

export function doStuff( container: Container ): void {
  var another = container.another;
  if ( another !== null && another.prop > 1 ) {
    another.prop = 2;
  }
}

@dcodeIO
Copy link
Member

dcodeIO commented Jan 12, 2020

To give an example of the challenges involved, this will compile just fine in TS, yet misses the null:

class Test {
  foo: string | null = null;
  get bar() {
    var foo = this.foo;
    this.foo = null;
    return foo;
  }
  set bar(a) {
    this.foo = a;
  }
}

function test() {
  var t = new Test();
  t.bar = "123";
  if (t.bar) {
    console.log(t.bar.toString()); // no type check error, yet a runtime error
  }
}
test();

@webmaster128
Copy link

webmaster128 commented Jan 20, 2020

I have another false positive that is simpler:

  // src/index.ts line 38-39
  const nullable: string | null = "a";
  const nonnullable: string = nullable || "fallback";

fails to compile with

ERROR TS2531: Object is possibly 'null'.

   const nonnullable: string = nullable || "fallback";
                               ~~~~~~~~~~~~~~~~~~~~~~
 in src/index.ts(39,30)

ERROR: Compile error

But the expression nullable || "fallback" can never be null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants