Microsoft/TypeScript

HTMLFormControlsCollection namedItem should return only form input elements

Open

#39,003 建立於 2020年6月10日

在 GitHub 查看
 (3 留言) (7 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: lib.d.tsHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

TypeScript Version: 3.9.2

Search Terms: HTMLFormControlsCollection RadioNodeList namedItem

Code

declare const form: HTMLFormElement;

const element = form.elements.namedItem('foo')

if (element && !(element instanceof RadioNodeList)) {
  console.log('eee', element.value) // <<< Property 'value' does not exist on type 'Element'
}

Expected behavior: According to MDN, namedItem() on a HTMLFormControlsCollection only returns a specific set of element types.

  • HTMLFormControlsCollection should extend HTMLCollectionOf<HTMLButtonElement | HTMLFieldSetElement | HTMLInputElement | HTMLObjectElement | HTMLOutputElement | HTMLSelectElement | HTMLTextAreaElement>
  • HTMLFormControlsCollection.namedItem() should return RadioNodeList | HTMLButtonElement | HTMLFieldSetElement | HTMLInputElement | HTMLObjectElement | HTMLOutputElement | HTMLSelectElement | HTMLTextAreaElement | null
  • RadioNodeList should extend NodeListOf<HTMLButtonElement | HTMLFieldSetElement | HTMLInputElement | HTMLObjectElement | HTMLOutputElement | HTMLSelectElement | HTMLTextAreaElement>

Actual behavior: HTMLFormControlsCollection.namedItem() return type is not specific enough (Element).

Playground Link: https://www.typescriptlang.org/play/?ssl=7&ssc=2&pln=1&pc=1#code/CYUwxgNghgTiAEYD2A7AzgF3gMyTAtgFzwASAKgLIAyAYnvgKIQj4goYDcAUF8uliGat28ALw56AOkEs2GNJJRRWwAJIYWACgDkuJNoCUPAJbZ4mmcKwAya-ACEFoXPjH+UFGBBIzAJSjAxkgAckigVMaYBgbwAN5c8IioaEjMkhBIAOY6ILnaADTwlnKSAG5QEACuIEYAvlxAA

Related Issues: #19437

貢獻者指南