Command Palette

Search for a command to run...

Hectal
Phase 3: Type Aliases and Interfaces
TypeScript · Exercise 5 of 6 PreviousNext
Intermediate

Word Counter

countWords builds a dictionary from each word to how many times it appears. Strict mode reports counts[word] because {} has no keys.

Give counts a dictionary type so the code type-checks. The checks also make sure that looking up a word that isn't in the result has the type number | undefined, not plain number.

What Check tests

  1. Your code has no type errors
  2. The console prints { to: 2, be: 2, or: 1, not: 1 }
  3. A result value can be read as a number-or-undefined
  4. A looked-up count is not assumed to exist

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Console

Nothing printed yet.