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

instanceof should error when all constituents of a union are primitives #18519

Closed
DanielRosenwasser opened this issue Sep 16, 2017 · 5 comments
Closed
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Sep 16, 2017

function foo(x: number | string) {
    if (x instanceof Number) {
        x
    }
}

Expected:

The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter

Actual:

No error, incorrect narrowing behavior (#14426)

Edit: Corrected the example to actually be a union.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Sep 16, 2017
@ajafff
Copy link
Contributor

ajafff commented Sep 16, 2017

That's not always the expected behavior. When targeting a runtime with Symbol.hasInstance this should be allowed:

class PrimitiveNumber {
    static [Symbol.hasInstance](x) {
        return typeof x === 'number';
    }
}
console.log(123 instanceof PrimitiveNumber); // true

@DanielRosenwasser
Copy link
Member Author

Sure, but

  1. Most people aren't doing that.
  2. We already error for 10 instanceof Number

@aluanhaddad
Copy link
Contributor

Wouldn't an appropriate behavior then be to issue an error unless the value on the right hand side has a [Symbol.hasInstance] member taking a parameter of a type to which the type of value on the left hand side is assignable?

@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Sep 16, 2017

Something like #17360? Ironically, I think I've brought it up with @mhegazy in the past.

@mhegazy mhegazy added the Help Wanted You can do this label Sep 18, 2017
@mhegazy mhegazy added this to the Community milestone Sep 18, 2017
charlespierce added a commit to charlespierce/TypeScript that referenced this issue Oct 10, 2017
@charlespierce
Copy link
Contributor

charlespierce commented Oct 10, 2017

PR #19063 opened to resolve this issue.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 28, 2017
mhegazy pushed a commit that referenced this issue Nov 28, 2017
* Error when LHS of instanceof is Union of Primitives #18519

* Refactor to allTypesAssignableToKind method and update test

* Use ternary expression instead of if / else blocks
@mhegazy mhegazy modified the milestones: Community, TypeScript 2.7 Nov 28, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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 Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

5 participants