Skip to content

false negative on uninitialized reference (null pointer exception) in class method #41446

Closed
@beauxq

Description

@beauxq

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

class B {
    public foo() {}
}

class C {
    private b: B;
    constructor() {
        this.reset();
        this.b = new B();
    }

    private reset() {
        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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions