Type events and refs
Type events by their control
Inline callbacks often receive an inferred event type from the JSX attribute. Extracted handlers need an annotation when context is lost. React event types describe the handler contract; the DOM node type specifies the relevant element. Use currentTarget for the element to which this handler is attached. It is not necessarily the original event target when events bubble.
import React, {useState} from 'react';import type {ChangeEvent} from 'react';export default function App(){ const [query,setQuery]=useState(''); const titles=['Ceramic bowl','Bronze lamp','Clay tile']; function change(event: ChangeEvent<HTMLInputElement>){setQuery(event.currentTarget.value);} const visible=titles.filter(t=>t.toLowerCase().includes(query.trim().toLowerCase())); return <section><label>Filter exhibits <input value={query} onChange={change}/></label> <ul>{visible.map(t=><li key={t}>{t}</li>)}</ul>{visible.length===0&&<p>No results</p>}</section>;}Initially all three titles appear. Typing clay shows Clay tile only. Input value is a string even when a control displays a numeric quantity; use an explicit conversion and validation when needed. ChangeEvent<HTMLInputElement> is suitable here, while a select uses HTMLSelectElement. The state type is inferred from the initial string.
Give component callbacks a domain contract
import React, {useState} from 'react';import type {FormEvent} from 'react';type EditorProps={onSave:(title:string)=>void};function Editor({onSave}:EditorProps){ const [text,setText]=useState(''); function submit(e:FormEvent<HTMLFormElement>){e.preventDefault();const title=text.trim();if(title){onSave(title);setText('');}} return <form onSubmit={submit}><label>Title <input required value={text} onChange={e=>setText(e.currentTarget.value)}/></label><button>Save</button></form>;}export default function App(){const [saved,setSaved]=useState<string|null>(null);return <><Editor onSave={setSaved}/><p role="status">{saved===null?'Nothing saved':saved}</p></>;}The status starts at Nothing saved. Enter Lamp and submit; the status changes to Lamp and the input clears. A whitespace-only title does not save. The callback receives a title, not a browser event, so the parent is independent of the form markup. The explicit state union permits both initial absence and later text; useState(null) alone would infer only null under strict settings. The demo has no persistence or server submission.
A DOM ref may be absent
import React, {useRef,useState} from 'react';export default function App(){ const inputRef=useRef<HTMLInputElement>(null); const [show,setShow]=useState(true); const [message,setMessage]=useState(''); function focus(){const input=inputRef.current;if(input===null){setMessage('Input is hidden');return;}input.focus();setMessage('Input focused');} return <section><button onClick={()=>setShow(v=>!v)}>{show?'Hide':'Show'} input</button> <button onClick={focus}>Focus input</button>{show&&<label>Search <input ref={inputRef}/></label>}<p role="status">{message}</p></section>;}Click Focus input to move keyboard focus to Search. Hide the field, then try again: the status reports Input is hidden. React assigns the node after mounting and clears it when removed. The guard narrows the nullable ref before calling focus. Do not write a non-null assertion merely to suppress a possible missing node; it changes no runtime behavior. The same ref type cannot safely be attached to a textarea. Browser focus and assistive-technology behavior are acceptance tests, not established by this type annotation.
Practice
Type a select change handler, a callback that accepts a selected room ID, and a textarea ref. Explain why a callback accepting a string should not be typed as a MouseEvent handler. Check the hidden-ref branch before trying to focus a node.