Command Palette

Search for a command to run...

Hectal
Phase 13: TypeScript in the Browser: DOM and Async
TypeScript · Exercise 3 of 6 PreviousNext
Intermediate

Run a function when Enter is pressed

Write onEnter(input: HTMLInputElement, run: (text: string) => void): void. When the user presses the Enter key in the input, call run with the input's current text. Other keys do nothing.

The page already calls your function for the input #box and logs got <text> from the callback.

Let TypeScript give the event its type. Do not annotate the event parameter.

What Check tests

  1. Your code has no type errors
  2. Enter calls the callback with the text
  3. Other keys do nothing
  4. A callback taking a number is rejected
  5. No annotation on the event

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.

Your page

Press Run to see the page.

Console

Nothing logged yet.