microsoft / microsoft/TypeScript

Allow primitive LHS to `instanceof` operator if RHS is a custom `hasInstance`

未关闭
#63,219 2 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看
Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 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.

贡献指南

打开贡献指南

调研方向

从链接的 TypeScript Playground 复现开始,将 `1 instanceof { [Symbol.hasInstance]() { return true }}` 的报告行为与预期结果进行比较。跟踪 `instanceof` 表达式的类型检查路径,然后添加覆盖用例,表明当右侧定义了自定义 `Symbol.hasInstance` 时,原始类型的左侧操作数会被接受。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
58/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。