microsoft / microsoft/TypeScript

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

Offen
#62,247 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Control Flow Help Wanted
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten TypeScript Playground und reproduziere den Unterschied zwischen dem unique-symbol-Aufruf und dem benannten isBar()-Aufruf in Foo.method(). Verfolge das Verhalten der Typ-Eingrenzung für beide abstrakten Typwächter und überprüfe anschließend, dass der symbolschlüsselbasierte Aufruf this auf Bar eingrenzt und den barMethod-Fehler nicht mehr meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.