You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// A *self-contained* demonstration of the problem follows...classFoo{publicfoo: string|null;bar(){constshouldDoSomething=this.foo!==null;if(shouldDoSomething){returnthis.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:
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...
The text was updated successfully, but these errors were encountered:
TypeScript Version: 2.2.1
The option
strictNullChecks
is enabled.Code
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:
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:
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...The text was updated successfully, but these errors were encountered: