TypeScript · Junior · Question 24 of 75

How do you specify the state type if the initial value is null: useState(null)?

Explanation

Without a generic, the type is inferred as null, and you won't be able to assign a user later. An explicit generic describes all possible states. The cast null as User lies to the compiler: the code will then assume a user always exists.

Further reading

Problem with a question

TypeScript · Junior · question 24

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz