Skip to content

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

Closed
@radarsu

Description

@radarsu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions