1. Concept library
  2. Web Dev with React
  3. Module 3: TypeScript with React

Concept

Typing hooks

useState and useRef infer their types from the initial value, so you only spell one out when the initial value does not show the full type: null before data arrives, or an empty array.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain when useState infers the type and when it needs a type argument
  2. Write useState for a value that starts as null and arrives later
  3. Spot the useState(null) that stays null forever

In practice

A few lines that show Typing hooks doing its job.

TSX
const [user, setUser] = useState<User | null>(null);
const [tags, setTags] = useState<string[]>([]);
const inputRef = useRef<HTMLInputElement>(null);

function useToggle(initial = false): [boolean, () => void] {
  const [on, setOn] = useState(initial);
  return [on, () => setOn((v) => !v)];
}

The common mistake

Watch for

Writing useState(null) with no type argument. The state is typed as null forever, and setUser(data) is a compile error.

What to know first

Your roadmap puts these before Typing hooks, unless you already know them.

How it stays learned

Once a session shows you understand Typing hooks, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Typing events