microsoft / microsoft/TypeScript
DOM: `Element#matches()` incorrectly narrows types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
### 🔎 Search Terms
element matches narrow never
### 🕗 Version & Regression Information
- This changed between versions 5.9.3 and 6.0.3
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/CYUwxgNghgTiAEEQBd4iQWxAO2QLngAkAVAWQBkBRTHZAbgCgBCdELXAOgymTAAsQAZwAU8AOTBkY+AEp4AMnloanbrwEjxwYNJlA
### 💻 Code
```ts
declare let element: HTMLElement;
!element.matches( 'dt' ) && element.matches( 'dd' )
```
### 🙁 Actual behavior
`element.matches( 'dd' )` throws a `Property 'matches' does not exist on type 'never'` compilation error.
### 🙂 Expected behavior
`element.matches( 'dd' )` does not throw any error.
### Additional information about the issue
`element.matches( 'dt' )` uses the following overload:
```ts
matches(selectors: K): this is HTMLElementTagNameMap[K];
```
The return type of `element.matches( 'dt' )` is then `this is HTMLElementTagNameMap['dt']` that maps to `this is HTMLElement`.
Since `this` is `HTMLElement`:
- When false, the type of `element` is `HTMLElement` (not narrowed further).
- When false, the type of `element` is `never`.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l'esempio collegato di TypeScript Playground ed esamina l'overload di Element#matches mostrato nell'issue, quindi segui come il predicato di tipo restringe HTMLElement dopo un controllo negato. L'issue è completa quando la seconda chiamata a element.matches('dd') viene compilata senza l'errore Property 'matches' does not exist on type 'never'.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Attiva
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 52/100