Skip to content

CFA: control flow for switch is defeated by outer if-else #11572

Closed
@ghost

Description

TypeScript Version: nightly (2.1.0-dev.20161012)

Code

enum E { A, B }

function f(e: E): number {
    switch (e) {
        case E.A: return 0
        case E.B: return 1
    }
}

function g(e: E): number {
    if (!true)
        return -1
    else
        switch (e) {
            case E.A: return 0
            case E.B: return 1
        }
}

Expected behavior:

Both functions succeed.

Actual behavior:

a.ts(10,19): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.

If I remove the else keyword (which makes an equivalent program), it compiles without errors.

If I put an e access under the switch, I see (parameter) e: never on hover, so that part is working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions