microsoft / microsoft/TypeScript

abstract type guard does not narrow when keyed with a unique symbol

未关闭
#62,247 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
Bug Domain: check: Control Flow Help Wanted
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

abstract type guard symbol

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about abstract type guard

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.0-dev.20250810&ssl=28&ssc=2&pln=1&pc=1#code/FAYw9gdgzgLgBASygIQIYCc4F44GUCeAtgEZgA2AFAERJrpUCUA3MMKsbOqiPCGalChwAYmDBwA3sDgy47Tt3i0MFBgC44MABZJEQui1lyOMLjzgBtZegC6qjdt26DrI4QCm2sABN7cAG5gCN6S0kayCABmcBSOUFYoGHYMDKHh6XAA9JlwACo6QlBaYACuZCEQYPDE7nIQcO7o6GDoYRmycQB0xBgAsp7Fvsxt4QC+DWRQtVExXdaqqVLtRl096P1eQ4YZoyO7u6D8gnB0DQAeMO4Q3kKi4kuyYP6N6MHTieh+cXonGGkZ6E8JXQ9VMJXc2xkByMTxeb0s1mSDgKP1OD3SgJgwNB6HBkLg0Nkaw2gz8gWCkn2QA

### 💻 Code

```ts
const isBar = Symbol("isBar");

abstract class Foo {
abstract isBar(): this is Bar;
abstract [isBar](): this is Bar;

method(): void {
if (this[isBar]()) {
// This should not be an error, it should work the same as .isBar()
this.barMethod();
} else if (this.isBar()) {
this.barMethod();
}
}
}

class Bar extends Foo {
override isBar(): this is Bar {
return true;
}

override [isBar](): this is Bar {
return true;
}

barMethod(): void {}
}
```

### 🙁 Actual behavior

TS reports an error:

> `Property 'barMethod' does not exist on type 'Foo'. Did you mean 'method'?(2551)`

### 🙂 Expected behavior

No error should happen here, the type guard should work the same as the non-symbol version of the method.

### Additional information about the issue

_No response_

贡献指南

打开贡献指南

调研方向

从链接的 TypeScript Playground 开始,重现 Foo.method() 中 unique-symbol 调用与命名的 isBar() 调用之间的差异。跟踪两个抽象类型保护的类型收窄行为,然后验证基于符号键的调用会将 this 收窄为 Bar,并且不再报告 barMethod 错误。

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

评估

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

把新 issue 发到你的邮箱

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