Skip to content

Erroneous type narrowing in try / catch / finally with strictNullChecks #18916

Closed
@davidni

Description

@davidni

TypeScript Version: 2.4.1

Code
Compile with strictNullChecks = true
Playground link: here (make sure to enable strictNullChecks)

class A {
    a: string;
}

let foo: A | undefined = void 0;
try {
    foo = new A();
    throw new Error();
} catch (e) {
    if (foo) {
        foo.a; // ERROR: Property 'a' does not exist on type 'never'.
    }
}
finally {
    if (foo) {
        foo.a; // ERROR: Property 'a' does not exist on type 'never'.
    }
}

Expected behavior:
Should compile without errors

Actual behavior:
Compilation fails at the indicated lines. Control flow analysis assumes that foo is of type undefined as it enters the try / catch / finally block, and is unable to detect that foo will actually be of type A in both the catch and finally blocks.

Likely related to #15835 and #18478

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions