-
-
Notifications
You must be signed in to change notification settings - Fork 672
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
Comments
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;
}
} |
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(); |
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
But the expression |
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:
This was not a case previously and not a case in the current version of typescript.
The text was updated successfully, but these errors were encountered: