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

Strict checks fail with switch(true) #37966

Closed
kaaboaye opened this issue Apr 14, 2020 · 1 comment
Closed

Strict checks fail with switch(true) #37966

kaaboaye opened this issue Apr 14, 2020 · 1 comment

Comments

@kaaboaye
Copy link

kaaboaye commented Apr 14, 2020

TypeScript Version: 3.7.x-dev.201xxxxx

Code

Minimal reproduction

function foo(someString: string | undefined) {
    switch (true) {
        case someString !== undefined:
            // this line is broken
            // it requires !non null assertion even though I've checked it above
            return someString.toUpperCase()

        default:
            return 'some default value'
    }
}

Expanded reproduction

// broken function
function foo(isSomething: boolean, someString: string | undefined) {
    switch (true) { 
        case isSomething:
            return 'is something'

        case someString !== undefined:
            // this line is broken
            // it requires !non null assertion even though I've checked it above
            return someString.toUpperCase()

        default:
            return 'some default value'
    }
}

// does the same staff as `foo` but has different implementation
function bar(isSomething: boolean, someString: string | undefined) {
    if (isSomething) {
        return 'is something'
    }

    if (someString !== undefined) {
        return someString.toUpperCase()
    }


    return 'some default value'
}

// ensure that the implementations are equivalent
([[true, undefined], [false, undefined], [true, 'asd'], [false, 'asd']])
    .forEach(([isSomething, someString]: any) =>
        console.assert(
            foo(isSomething, someString) === bar(isSomething, someString),
            'test feild for', isSomething, someString
        )
    )

Expected behavior:

Compile just fine

Actual behavior:

Throws

(parameter) someString: string | undefined
Object is possibly 'undefined'.(2532)

for

        case someString !== undefined:
            return someString.toUpperCase()

Playground Link:

https://www.typescriptlang.org/play/#code/PTAECMCcHsGsFMB2oBmBXRBjALgS2ogFDpZ4GrTQAUuAzgMrQC282AFrogOYBcElAG3gBDRABpQtZvHrZInXpLkLQAH1AYAJvBSd4mgJSgA3oVDnJAd1zZMbUFTlp4R46DMXPmYbXig6jCzsCjwenuGgkKxokMgA5HSS0sHccYRh4d6+SSyy8tygAIQAvMUaiNq6iPqhEREgoMG0oAJ6-s1QcEgZdaANNpHwAI5ouFHNhYjkiGgCAqA+vpBkyPAAbkiNbNBoXPYAknEboHbwmAia-tgL4NAbPXVR2DHIUrnK3AB02NAAqgAO-3gkAAwj54FQDOlepVhLNsLVehYni9QHE3n5YfDQGthAJnGlPABfQgkwgNTTQeDNdh+WjCFhKYQoFALZoAAxQlHZEDQ1zYPlAmlwLOBSGuuCY-yELEQ2GEK2IGBw+GQ4GEkBoDGSHG4fFu0CEogkGLyISU+S4anKlT0hhMGRFDgCOoUrge5hRsTRiQxKS4hIsZM8Tqopo+VpKZS0Ojt7t6Xte0jNXx+AKBoPBkIyZIyibRGKFOjhAmuuPx8DSZIaSFoMT87AVWz8kul8Fl8pWzQ1fmGo3L4sIVAA2sOnPAJDGqvoALoSYcoPG+ScVWPVTRz0BjyDOCRxHyaOKbhdLidog9HmdQzyfLmQACiwjsVBHLqCuq4JuTEZnfFEAE8jGKAA+D0TgIKQhE+RZgWwKgwM8LlqDfVgPy-d5LSA0oIA1LVAlQhR0JkCMDDEBCLDibBqWuFB4FwARLjvOIJBQ-0iJTLgwOvCwoUIIA

@IllusionMH
Copy link
Contributor

Duplicate of #8934

Recent discussion #37178

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants