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

type checking after instainceOf condition in else block is working incorrectly #5523

Closed
stepancar opened this issue Nov 4, 2015 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@stepancar
Copy link

see example in playground

interface IFoo {
    value: string[]|string;
}

function bar(options: IFoo) {
    var val = options.value;
    var result: string;
    if (val instanceof Array) {
        result = val.join('')
    } else {
        result = val;
    }
}

val has one of two types strting[] or string
in 'if' condition checked instanceofArray
in else statement compiler should know that val has string type

@sandersn
Copy link
Member

sandersn commented Nov 4, 2015

#5442 should fix this, I think.

@weswigham
Copy link
Member

#5442 only has changes typeof type guards and composite type guards.

But this behavior is actually by spec - with instanceof type guards, we only affirmatively narrow (so in the true branch) - the logic being that since TS uses structural typing, even if instanceof catches something, instanceof not catching something doesn't mean that the thing you're checking is not a given type. (In effect, val instanceof Array does not imply that val does not have the type of an array, since it could still have the shape of an array but not the same constructor.) User-defined type guards are more strict, as we assume when you make a type predicate that it's capable of being exhaustive, and typeof is more strict because primitives are effectively nominal.

In the example above, if you do this:

interface IFoo {
    value: string[]|string;
}

function bar(options: IFoo) {
    var val = options.value;
    var result: string;
    if (typeof val !== "string") {
        result = val.join('')
    } else {
        result = val;
    }
}

it will narrow as you desire. Prefer typeof to instanceof where possible because of this.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 4, 2015
@RyanCavanaugh
Copy link
Member

#1719 is the issue for this

@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

4 participants