microsoft / microsoft/TypeScript
Allow primitive LHS to `instanceof` operator if RHS is a custom `hasInstance`
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- PR merge metrics
- PR metrics pending
Description
### 🔎 Search Terms
in:title instanceof
### 🕗 Version & Regression Information
- This changed between versions 5.2 and 5.3
- This changed in commit or PR #55052
### ⏯ Playground Link
https://www.typescriptlang.org/play/?#code/IwAglgdgzgLghhAxgUwPYDMQG8QG0DKAngLYBGqANgHQAWcUAktPEsgLoAUAlNiAE7IYAVz4QQMPkOQgAvjICwAKAD0ykAD0QAFRrSKydDAC0dCABMQUMGekYQCEAHJIsBCgyOQyAB4AHAVBWqGLEQrAgpLaYMIS+0o4IhI4ANPZiqKQAVsiIMOKxtnz2+XEgvnB8cMSCyHxUHABMAMwArAAcXEA
### 💻 Code
```ts
1 instanceof { [Symbol.hasInstance]() { return true }}
```
### 🙁 Actual behavior
Error: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.(2358)
### 🙂 Expected behavior
No errors
### Additional information about the issue
For custom 'Symbol.hasInstance' methods, `number instanceof obj` can return true or false according to implementation.
Contributor guide
Research direction
Start with the linked TypeScript Playground reproduction and compare the reported behavior for `1 instanceof { [Symbol.hasInstance]() { return true }}` with the expected result. Trace the type-checking path for `instanceof` expressions, then add coverage showing that a primitive left-hand side is accepted when the right-hand side defines a custom `Symbol.hasInstance`.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100