Microsoft/TypeScript

Generic typing used for "input" event listeners

Open

#54,336 建立於 2023年5月21日

在 GitHub 查看
 (3 留言) (2 反應) (0 負責人)TypeScript (6,726 fork)batch import
Domain: lib.d.tsHelp WantedPossible Improvement

倉庫指標

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

描述

# Bug Report

🔎 Search Terms

typescript input event ts InputEvent typescript addEventListener("input",

🕗 Version & Regression Information

I just found it today but verified it was present back to 3.3.3333 I also verified it existed on nightly

Please keep and fill in the line that best applies:

This is the behavior in every version I tried, and I reviewed the FAQ for entries about... I looked at entries about event listeners, HTML element types, and callbacks.

⏯ Playground Link

Demo Playground Link

💻 Code

// Error on the typing of the event parameter
(null as any as HTMLInputElement).addEventListener("input", (e: InputEvent) => {});
(null as any as HTMLTextAreaElement).addEventListener("input", (e: InputEvent) => {});

🙁 Actual behavior

The sample code fails to compile because the input event is always typed as an Event, not InputEvent.

🙂 Expected behavior

For most elements, the callback event should be Event, but for <textarea> and <input>, it should be InputEvent.

According to MDN

For and elements that accept text input (type=text, type=tel, etc.), the interface is InputEvent; for others, the interface is Event.

According to the W3C spec, it should be InputEvent for any element that is contenteditable. I don't know that you can trigger an input event on other non-contenteditable elements so perhaps the event type can be made InputEvent, regardless of the element type it is called on.

貢獻者指南