Event Types Annotating Event Types

Last updated: 2026-11-02

Event Types Annotating Event Types

Event types come from @types/react: onClick is typed as MouseEvent<HTMLButtonElement> and onChange as ChangeEvent<HTMLInputElement>. Once they are annotated, currentTarget offers precise hints about the element you are working with instead of a generic Element. Annotating an inline handler is optional, because the JSX attribute already provides the type for you. For a handler you pull out into its own function, write the annotation by hand and the editor keeps checking it for you.

GroupData & Engineering
KindPattern

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

function SearchBox() {
  const [kw, setKw] = useState('')

  const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setKw(e.currentTarget.value)   // currentTarget is inferred as HTMLInputElement
  }
  const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault()
  }
  return (
    <form onSubmit={onSubmit}>
      <input value={kw} onChange={onChange} />
    </form>
  )
}
// The inline form needs no annotation: onChange={e => setKw(e.target.value)} - e is inferred

Returns:No return value; this is a coding pattern rather than a callable API.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Typing logs ChangeEvent<HTMLInputElement> and clicking logs MouseEvent<HTMLButtonElement>, with the last four entries listed below - at runtime these are ordinary events, the TypeScript annotations live in the syntax block.

💡 Key Points

💡 Tip In the inline form onChange={e => …} the parameter e is inferred, so you only need an explicit annotation when you extract the handler into its own function.
ℹ️ Info The synthetic event types take a type parameter that names the element: without it you get Element, and with a concrete element the hints for currentTarget get much sharper.
⚠️ Warning The type of e.target is broad (it may be any element), so check the element before reading value; e.currentTarget is the element the handler is bound to and its type is always precise.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Why not use the native MouseEvent type?
A React events are synthetic events (a cross-browser wrapper) and the native types do not carry the React generic inference; mixing them gives wrong hints. Use React.MouseEvent and friends consistently.
Q How do I type the onChange of my own component?
A It follows the props of your component: type Props = { onChange: (v: string) => void }. The caller then infers e from your signature and needs no synthetic event type at all.
Q Why is e.currentTarget null in an async callback?
A currentTarget is set to null once the handler returns. Save it first - const el = e.currentTarget - and use el inside the callback.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏