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

Early return instead of "else" causes compiler error. #8365

Closed
radarsu opened this issue Apr 29, 2016 · 2 comments
Closed

Early return instead of "else" causes compiler error. #8365

radarsu opened this issue Apr 29, 2016 · 2 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@radarsu
Copy link

radarsu commented Apr 29, 2016

TypeScript Version:

nightly (1.9.0-dev.20160217)

Code

let greeting:string;
let x = (message: string | number) => {
        if (typeof message !== "string") {
            return;
        }
        // It's obvious, that message must be a string now. It works with "else statement" but not with early return.
        greeting = message;
    }
}

Expected behavior:
No raising error.

Actual behavior:
Compiler raises an error that type string | number cannot be assigned to string variable.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 29, 2016

This is working correctly in latest after #8010

@malibuzios
Copy link

I tried this in 1.9.0-dev.20160429 (the latest nightly) and it does perform the analysis.

I believe version 1.9.0-dev.20160217 (a nightly build from 17th of February 2016) may not yet include control flow based type analysis, that was only merged on the 23 of April.

@mhegazy mhegazy added Bug A bug in TypeScript Fixed A PR has been merged for this issue labels Apr 29, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Apr 29, 2016
@mhegazy mhegazy closed this as completed Apr 29, 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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants