Command Palette

Search for a command to run...

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

A Generic Result Type

safeDivide returns a Result that is either a success with a number or a failure with a text. The type is written for numbers only, so it can't be reused.

Make Result generic: Result<T, E> with E defaulting to string. Then safeDivide returns Result<number>. The checks use Result with other value and error types.

What Check tests

  1. Your code has no type errors
  2. The console prints 5 and divide by zero
  3. Result<number> accepts a success
  4. Result<string, Error> accepts an Error failure
  5. A text where a number is promised is rejected
  6. The default error type is string

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.