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

Types defining Symbol.hasInstance should be eligible to use with the instanceof operator #39064

Closed
lazytype opened this issue Jun 14, 2020 · 5 comments · Fixed by #55052
Closed
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@lazytype
Copy link

TypeScript Version: 3.9.4

As specified here, https://tc39.es/ecma262/#sec-instanceofoperator, it should be sufficient for an object to define a function for the Symbol.hasInstance well-known symbol in order to be used as the right-hand side of the instanceof operator.

It seems like this was intended to be implemented based on these comments: #12728 (comment)
but for whatever reason that didn't happen it in the PR

Search Terms: Symbol.hasInstance

Code

({}) instanceof {[Symbol.hasInstance]: () => true}
// Running this in the browser evaluates to `true`

Expected behavior:
No type-errors

Actual behavior:

({}) instanceof {[Symbol.hasInstance]: () => true}
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2359: The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type.

Playground Link: Playground Link

Related Issues:

@lazytype
Copy link
Author

I have a tested and working branch that implements this proposal here: https://github.com/lazytype/TypeScript/commit/0c226a775f63f4dd97f6a65422707d4231787188

Holding off on creating a PR until this issue gets tagged with the "Backlog" milestone, per the contributor guidelines.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jun 16, 2020
@ExE-Boss
Copy link
Contributor

See also: #17360

@rotu
Copy link

rotu commented Jan 29, 2023

The typescript compiler also spuriously complains when the lhs of instanceof is not an object:

class MyNumber {
  static [Symbol.hasInstance](x:any) {
    return (typeof x === 'number');
  }
}

console.log(1 instanceof MyNumber);

It gives this error:

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

@shrinktofit
Copy link

Why was this issue marked as "Suggestion"? I think it should be labeled as bug. @RyanCavanaugh

@MichaelMitchell-at
Copy link

I have a tested and working branch that implements this proposal here: lazytype@0c226a7

Holding off on creating a PR until this issue gets tagged with the "Backlog" milestone, per the contributor guidelines.

❤️ Thanks for finally fixing the bug @rbuckton. I can finally delete this branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants