Skip to content
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

Erroneous "Object is possibly null" message #14889

Closed
akanix42 opened this issue Mar 27, 2017 · 4 comments
Closed

Erroneous "Object is possibly null" message #14889

akanix42 opened this issue Mar 27, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@akanix42
Copy link

TypeScript Version: 2.2.1

The option strictNullChecks is enabled.

Code

// A *self-contained* demonstration of the problem follows...
class Foo {
  public foo: string | null;

  bar() {
    const shouldDoSomething = this.foo !== null;

    if (shouldDoSomething) {
      return this.foo.length;
    }
  }
}

Expected behavior:
There should not be an error, since I have just checked that the object isn't null. This should have the same effect as the following:

class Foo {
  public foo: string | null;

  bar() {
    if (this.foo !== null) {
      return this.foo.length;
    }
  }
}

Actual behavior:
TypeScript throws an error on the foo object: "TS2531 Object is possibly null".

The workaround I'm using follows, but isn't what I'd consider ideal:

class Foo {
  public foo: string | null;

  bar() {
    const shouldDoSomething = this.foo !== null;

    if (shouldDoSomething) {
      return (<string>this.foo).length;
    }
  }
}

As a real example, I have a boolean shouldEmitToOccupant that determines whether or not an event should be emitted to an occupant of a certain tile in my game. This variable is only true if several tests are passed, the first of which is the null check. For now I'm using an assertion, but it seems like I shouldn't have to...

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Mar 27, 2017

Same response as #10830. #10421 would be a good solution.

What I usually do is move out the null check to an early return:

class Foo {
  public foo: string | null;

  bar() {
    if (this.foo === null) return;
    const shouldDoSomething = (more checks...);

    if (shouldDoSomething) {
      return this.foo.length;
    }
  }
}

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 27, 2017
@aluanhaddad
Copy link
Contributor

aluanhaddad commented Mar 27, 2017

Why not use a local const binding? If foo were a getter there would be no way to know that it would not return null the next time it was evaluated.

@RyanCavanaugh
Copy link
Member

That wouldn't help since there's still no tracking between shouldDoSomething and the local const's nullness

@aluanhaddad
Copy link
Contributor

@RyanCavanaugh gotcha, thanks for explaining.

@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 21, 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

4 participants