Answers
Here are the answers to the questions in the preceding section.
- The
name
prop will have theany
type. - The
firstName
state will be given thestring
type becausestring
will be inferred from the initial value""
. - There will be no type error even though
firstName
is not passed because it is defined as optional. - The inferred type of
status
isstring
. An explicit type can be defined for the state using a generic type argument as follows:const [status, setStatus] = useState<'Good' | 'Bad'>('Good');
- The type for the
FruitList
component could be as follows:type Props = {
fruits: string[];
}
Alternatively, it could be defined using an interface as follows:
interface Props { fruits: string[]; }
- The
email
state could be defined as follows:const [email, setEmail] = useState<string | null>(null);
An explicit type needs to be defined; otherwise, an initial value of null...