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.

贡献者指南