microsoft / microsoft/TypeScript

DOM: `Element#matches()` incorrectly narrows types

オープン
#63,497 コメント 14 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Domain: lib.d.ts Help Wanted
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 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
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:

matches<K extends keyof HTMLElementTagNameMap>(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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた TypeScript Playground の例から始め、issue に示されている Element#matches のオーバーロードを調べ、否定チェックの後に型述語が HTMLElement をどのように絞り込むかを追跡します。2 回目の element.matches('dd') 呼び出しが、Property 'matches' does not exist on type 'never' エラーなしでコンパイルできれば、issue は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。