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
{{ message }}
Repository navigation
false negative on uninitialized reference (null pointer exception) in class method #41446
If a class constructor calls another method, references in that method are not checked for initialization.
TypeScript Version: 4.0.3
Search Terms: false negative null undefined used before being assigned
Code
classB{publicfoo(){}}classC{privateb: B;constructor(){this.reset();this.b=newB();}privatereset(){this.b.foo();// false negative - `b` used before being assigned}}
Expected behavior: error: b used before being assigned
Actual behavior: no error
Related Issues:
Note that this problem can be solved (mostly) with this: #30462
That issue talked about a false positive, but this false negative is also a problem that could be solved with the same solution.
It can't be solved completely because of possible complexities in structures.
But I ran into a null pointer exception that would have been caught by TypeScript if it just followed a simple single level of function calls.
This doesn't really tell us much, especially in the presence of virtual methods (which is going to be very common). Inlining one level instead of zero levels just creates "TypeScript isn't consistent about X" bugs.
It does not create "TypeScript isn't consistent about X" bugs.
All of those bugs are already here. It is already inconsistent about finding some "used before being assigned" bugs and not others.
If consistency is what you want, then you need to make it so you don't find any "used before being assigned" bugs.
It would not create any inconsistency. It would find more bugs.
this.b is not yet assigned when A.method is called during the construction of A, but after it is constructed, subsequent calls to A.method are perfectly fine.
Here's a Java version of same thing (results in NullPointerException):
Activity
RyanCavanaugh commentedon Nov 9, 2020
This doesn't really tell us much, especially in the presence of virtual methods (which is going to be very common). Inlining one level instead of zero levels just creates "TypeScript isn't consistent about X" bugs.
beauxq commentedon Nov 9, 2020
It does not create "TypeScript isn't consistent about X" bugs.
All of those bugs are already here. It is already inconsistent about finding some "used before being assigned" bugs and not others.
If consistency is what you want, then you need to make it so you don't find any "used before being assigned" bugs.
It would not create any inconsistency. It would find more bugs.
theonlypwner commentedon Apr 17, 2023
Doug Hoskisson (@beauxq) I noticed a similar issue that has a false negative. How can we handle something like this?
Playground: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgJICFkG9kFsJgAWA9gCYAUAlAFzIBuxwpyAvgFBsIA2cAzr8gCCyYLgAOXCPnACM2NskXIARsgC8yEBADuydOSLBelBUvxEyVbMkO8AdMrvmSFSqzbtOPfnpHjJ0mCymFimigjEILxgUACuCGDEUOTKtBhuoUpZKk4ELlYA9AXICFB8hBACsSCSPhG4gRDMxLFgYe5ZzpYZJZG8xJJ2XMQA5uQADG7snlq6glS5Fq5sQA
this.bis not yet assigned whenA.methodis called during the construction ofA, but after it is constructed, subsequent calls toA.methodare perfectly fine.Here's a Java version of same thing (results in
NullPointerException):