Set The Type For A useState Hook
TypeScript can often infer the type of a useState
hook. For instance, in the following example, TypeScript infers a type of boolean
:
If we have a useState
hook that can be null
or a string:
then we'll get a TypeScript warning when we violate the inferred type of SetStateAction<null>
.
The useState
can be appropriate typed for this situation like so:
Last updated