Skip to content

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

@lazytype

Description

@lazytype

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions