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

Narrowing strangeness with instanceof #10167

Closed
yortus opened this issue Aug 5, 2016 · 1 comment
Closed

Narrowing strangeness with instanceof #10167

yortus opened this issue Aug 5, 2016 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@yortus
Copy link
Contributor

yortus commented Aug 5, 2016

TypeScript Version: nightly (2.1.0-dev.20160804)

Code

function f(s: Set<string> | Set<number>) {

    // (1)
    s = new Set<number>();
    s // s is Set<number>
    if (s instanceof Set) { } // Clearly no side-effects here. No possible changes to s.
    s // s is Set<number> | Set<any>                                                 <===== (1a)
    s.add(42); // ERROR: Cannot invoke an expression whose type lacks call signature <===== (1b)

    // (2)
    s = new Set<number>();
    s // s is Set<number>
    if (s instanceof Promise) { } // Clearly no side-effects here. No possible changes to s.
    s // s is Set<number> | (Set<number> & Promise<any>)                             <===== (2a)
    s.add(42); // No error this time                                                 <===== (2b)
}

function g(s: Set<string> | Set<number>) {

    // (3)
    s // s is Set<string> | Set<number>
    if (s instanceof Set) {
        s // s is Set<string> | Set<number>
    }
    else {
        s // s is never                                                              <===== (3a)
        s.add(42); // ERROR 'add' does not exist on never                            <===== (3b)
    }

    // (4)
    s = new Set<number>();
    s // s is Set<number>
    if (s instanceof Set) {
        s // s is Set<any>
    }
    else {
        s // s is Set<number>                                                        <===== (4a)
        s.add(42); // no error, but we couldn't possibly have a Set instance here    <===== (4b)
    }
}

Expected behavior:

  • inferred type of s at (1a) is Set<number>
  • inferred type of s at (2a) is Set<number>
  • inferred type of s at (3a) is never
  • inferred type of s at (4a) is never
  • Compiler errors at (3b) and (4b)
  • No compiler errors at (1b) and (2b)

Actual behavior:

  • inferred type of s at (1a) is Set<number> | Set<any>
  • inferred type of s at (2a) is Set<number> | (Set<number> & Promise<any>)
  • inferred type of s at (3a) is never
  • inferred type of s at (4a) is Set<number>
  • Compiler errors at (1b) and (3b)
  • No compiler errors at (2b) and (4b)

Notes:

Brought over from #9862 which is closed. See that issue for the discussion leading to the above examples. Cc: @ahejlsberg, @sandersn

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Aug 7, 2016
@ahejlsberg ahejlsberg added this to the TypeScript 2.0.1 milestone Aug 7, 2016
@ahejlsberg ahejlsberg self-assigned this Aug 7, 2016
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Aug 8, 2016
@yortus
Copy link
Contributor Author

yortus commented Aug 9, 2016

Just tried the nightly - works great! Thanks for the brilliant work @ahejlsberg

@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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants