Skip to content
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

Control flow type analysis doesn't flow to a callback closure #9753

Closed
zakjan opened this issue Jul 15, 2016 · 3 comments
Closed

Control flow type analysis doesn't flow to a callback closure #9753

zakjan opened this issue Jul 15, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@zakjan
Copy link

zakjan commented Jul 15, 2016

TypeScript Version: 2.0.0 (installed today with npm install typescript@beta) with strictNullChecks enabled

Code

function plusOne(x: number): number {
    return x + 1;
}

function test(x?: number): Promise<number> {
    if (!x) {
        return Promise.resolve(0);
    }

    return Promise.resolve().then(() => plusOne(x));
}

Expected behavior:
no type error

Actual behavior:
error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
Type 'undefined' is not assignable to type 'number'.

@zakjan
Copy link
Author

zakjan commented Jul 15, 2016

However if the closure is used directly, it is ok.

function test2(x?: number): number {
    if (!x) {
        return 0;
    }

    return (() => plusOne(x))();
}

This emits no error.

@zakjan zakjan changed the title Control flow type analysis doesn't flow to a promise handler closure Control flow type analysis doesn't flow to a callback closure Jul 15, 2016
@kitsonk
Copy link
Contributor

kitsonk commented Jul 15, 2016

Essentially a dupe of #9263, #7719, #8541, #8552, #9491 (and many more...). The root reason is discussed here.

@zakjan
Copy link
Author

zakjan commented Jul 15, 2016

Ah, ok, thanks a lot.

@zakjan zakjan closed this as completed Jul 15, 2016
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 15, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

3 participants