arrow_backBACK TO DRILLS
Blue Beltstate-basicslockPREMIUM

useState Generic Without an Initial Value Infers T | undefined

This component declares state with an explicit generic type argument but no initial value.

REACT_MODULE // 0x42
const [isOpen, setIsOpen] = useState<boolean>();
function toggle() {
setIsOpen(prev => !prev);
}

TypeScript reports that prev inside the updater function has type boolean | undefined instead of boolean. Why?

lock

This Drill is Locked

This is a premium drill. Continue on to the next free drill in the sequence.