microsoft / microsoft/TypeScript

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

Offen
#63,219 2 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit der verknüpften TypeScript Playground-Reproduktion und vergleiche das berichtete Verhalten für `1 instanceof { [Symbol.hasInstance]() { return true }}` mit dem erwarteten Ergebnis. Verfolge anschließend den Pfad der Typprüfung für `instanceof`-Ausdrücke und füge dann eine Abdeckung hinzu, die zeigt, dass eine primitive linke Seite akzeptiert wird, wenn die rechte Seite ein benutzerdefiniertes `Symbol.hasInstance` definiert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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