This is ok:
const [foo, setFoo] = useState<string>()
This is not:
const [boo, setBoo] = useState<string>(undefined)
Argument of type ‘undefined’ is not assignable to parameter of type ‘string | (() => string)’
I think they have the same meaning, though – useState
with no arguments is the same as useState(undefined)
.
Is it simply that TypeScript can’t detect this for some reason?
2
Answers
Because the DefinitelyTyped definition is overloaded with 2 signatures for
useState
, where one of them has no arguments:If you want the argument definition to allow for an
undefined
you need to explicitly define it like so:useState<string>()
is a convinence shorthand foruseState<string|undefined>(undefined)
The typing is here
As stated in the code comment, this is just convinience, not an omission but put there on purpose.
Do not use this is your state must strictly be a
string
as omiting the argument makesundefined
to also be allowed.Sidepoint: this also allows
useState()
with no argument or type argument, which results in a state object that can only beundefined