Skip to content

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

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

Closed
davidni opened this issue Oct 3, 2017 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@davidni
Copy link

davidni commented Oct 3, 2017

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

@mhegazy
Copy link
Contributor

mhegazy commented Oct 3, 2017

Duplicate of #18478

@mhegazy mhegazy marked this as a duplicate of #18478 Oct 3, 2017
@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 3, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 19, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 19, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants