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

Control flow analysis could affect the scope of a type guard #7064

Closed
chuckjaz opened this issue Feb 12, 2016 · 2 comments
Closed

Control flow analysis could affect the scope of a type guard #7064

chuckjaz opened this issue Feb 12, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@chuckjaz
Copy link
Contributor

Consider,

function isString(obj: any): obj is string {
    return typeof typeof obj === "string"; 
}

function foo(value: (string|number)): number {
    if (isString(value)) {
        return value.indexOf('foo')
    } else
        return value;
}

function bar(value: (string|number)): number {
    if (isString(value)) {
        return value.indexOf('foo')
    }
    return value;
}

Expected: Both foo and bar type-check correctly.
Received: bar reports that value is not assignable to number.

Flow analysis could be used here to determine value can only be number in all the code paths that arrive at the final return.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 13, 2016

looks like a duplicate of #2388

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 13, 2016
@mhegazy mhegazy closed this as completed Feb 13, 2016
@chuckjaz
Copy link
Contributor Author

Search fail. Sorry about that.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants