Currently, the TypeScript compiler thinks the type of email
and password
are null
(and no other value). You can resolve this by providing an explicit type parameter to the useState
call so that the types of email
and password
are known to be string
or null
.
const { useState } = React;
function Example() {
const [state, setState] = useState<{email: null | string, password: null | string}>({ email: null, password: null });
function setEmail(email: string) {
setState(prevState => ({ ...prevState, email }))
}
return <p>{state.email}</p>
}