Command Palette

Search for a command to run...

Hectal
Phase 7: Generics
TypeScript · Exercise 4 of 6 PreviousNext
Intermediate

A Generic Stack

The Stack class stores unknown, so every value you take out has to be checked. The program already writes new Stack<number>(), which is an error until the class has a type parameter.

Make Stack generic. A Stack<number> must refuse text, and pop() must return number | undefined.

What Check tests

  1. Your code has no type errors
  2. The console prints 2 1
  3. A Stack<string> accepts text
  4. A Stack<number> refuses text
  5. pop() returns number | undefined
  6. pop() is not a plain number

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.