React Hooks with TypeScript/Generic useState type

Annotate nullable or union state explicitly.

Section: TypeScript with State and Refs

Generic useState type

tsx
tsx
const [user, setUser] = useState<User | null>(null);
Explanation

Useful when TypeScript cannot infer the full intended state type from the initializer.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing TypeScript with State and Refs
Type a DOM ref
Specify the element type for refs.
OpenIn sheettsxsame section
Discriminated union reducer actions
Type reducer actions cleanly with a union.
OpenIn sheettsxsame section
Typed tuple return
Return readonly tuples for hooks that act like useState.
OpenIn sheettsx3 tag match
Generic async hook result type
Type reusable async hook data generically.
OpenIn sheettsx3 tag match