Skip to content

noImplicitReturns control flow errors with explicit type guard checks #23271

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
tomrav opened this issue Apr 9, 2018 · 5 comments
Closed

noImplicitReturns control flow errors with explicit type guard checks #23271

tomrav opened this issue Apr 9, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@tomrav
Copy link

tomrav commented Apr 9, 2018

TypeScript Version: 2.8.1 & 2.9.0-dev.20180409

Search Terms: noImplicitReturns throw type guard

Code

interface A { type: 'A' }
interface B { type: 'B' }

function isA(val: any): val is A { return val && val.type === 'A'}
function isB(val: any): val is B { return val && val.type === 'B'}

function getAOrThrow(val: A | B): A {
    if (isA(val)) { 
        return val; // val inferred to A
    } else if (isB(val)){ // val inferred to B
        throw new Error(); 
    }
    val // type inferred to never
}

Expected behavior:
Type check should pass without error.

Actual behavior:
Happens only with noImplicitReturns turned on.
getAOrThrow errors on its return value with Function lacks ending return statement and return type does not include 'undefined'..
If I remove the second type guard call (isB) and just leave it as a catch-all else, there is no error.

Playground Link:
Cannot reproduce in playground due to noImplicitReturns being turned off.

Related Issues:
Couldn't find any.

@tomrav
Copy link
Author

tomrav commented Apr 10, 2018

Seems similar to #23281

@tomrav
Copy link
Author

tomrav commented Apr 10, 2018

Thanks for the tip, didn't know you could do that! 👍

@mhegazy
Copy link
Contributor

mhegazy commented Apr 10, 2018

Please see #17358, #20409, #10470 and #9655

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 10, 2018
@tomrav
Copy link
Author

tomrav commented Apr 11, 2018

I see how you're approaching it now, I'll close the issue.
Thanks 👍

@tomrav tomrav closed this as completed Apr 11, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants