State, and the Four States Again
State is usually inferred. useState(0) is a number and needs nothing.
Annotate when the initial value is not representative, which in practice means null: useState<Book | null>(null) , because useState(null) infers null and nothing else, and every later setBook(realBook) is then an error.
And here the course closes a loop. The four states from the React course, the reducer from module 12 of that course, and the discriminated union from module 6 of this one are all the same idea arriving from three directions.
With a discriminated union, loading carrying an error message is not a bug you avoid. It is a value that cannot be constructed.
Mark this lesson complete
Signed in, your progress follows you to every device.