React Reference ยท JSX Syntax and Rules
JSX with TypeScript
How to type React components in .tsx files: props interfaces, children, ReactNode versus ReactElement, event handlers, and the tsconfig jsx setting.
TypeScript understands JSX natively when the file has the .tsx extension. It checks each tag against the component's declared props, so a misspelled prop, a missing required prop or a wrong value type becomes an editor error before the code runs. The types are erased during compilation; at run time you still have plain JavaScript.
Three pieces cover most day-to-day work: describing props with a type or interface, choosing the right type for children and returned values, and typing event handlers. The examples here are read-only because the site's runner transpiles JSX only, not TypeScript, and the live demos below show the runtime behavior those types describe.
import type { ReactNode } from 'react'; type ButtonProps = { label: string; variant?: 'primary' | 'quiet'; onPress: () => void; children?: ReactNode; }; function Button({ label, variant = 'primary', onPress, children }: ButtonProps) { return ( <button className={variant} onClick={onPress}> {label} {children} </button> ); } <Button label="Save" onPress={() => {}} />; // ok <Button label="Save" variant="loud" />; // error: bad variant, onPress missing
Try it
Syntax
Common types, imported with import type { ... } from 'react':
| Type | Use it for |
|---|---|
ReactNode |
Anything React can render: elements, strings, numbers, arrays, null, booleans. Best for children props. |
ReactElement |
The object JSX produces; use when you need an actual element, for example to clone it. |
ComponentProps<typeof X> |
The props of component or tag X, such as ComponentProps<'button'>. |
PropsWithChildren<P> |
P plus an optional children. |
MouseEvent<HTMLButtonElement> |
The event argument of an onClick on a button. |
ChangeEvent<HTMLInputElement> |
The event argument of an onChange on an input. |
CSSProperties |
A style object. |
function Field(props: React.ComponentProps<'input'> & { label: string }) { const { label, ...inputProps } = props; return <label>{label} <input {...inputProps} /></label>; }
tsconfig: use "jsx": "react-jsx" (automatic runtime) or "react-jsxdev" for development. "jsx": "preserve" leaves JSX for another tool such as a bundler.
Usage notes
- Skip
React.FC. A plain function with a typed props parameter infers the return type and avoids implicitchildrenassumptions from older typings. - Return type. Components may return
ReactNode; you rarely annotate it. - Generic components work:
function List<T>(props: { items: T[]; render: (item: T) => ReactNode }). In.tsxwrite<T,>for arrow functions to avoid parsing the type parameter as a tag. - Event types let you read
event.currentTarget.valuesafely; prefercurrentTargetovertargetfor typed access. - Do not cast to silence errors with
as any; fix the prop type or use a union. - Typed
keyandref.keyis never part of props; in React 19refis an ordinary prop, so declare it (ref?: Ref<HTMLInputElement>) when you accept one. .tsxversus.ts. Only.tsxmay contain JSX; type assertions with angle brackets (<T>value) are not allowed there, usevalue as T.