Command Palette

Search for a command to run...

Hectal
Phase 15: TypeScript in Real Projects
TypeScript · Exercise 4 of 6 PreviousNext
Intermediate

A client typed by the contract

A Contract lists two routes with their request and response types, and handlers implements them. Write call<R extends Route>(route: R, request: Contract[R]["request"]) that returns Contract[R]["response"] by running the matching handler.

The route name must decide the types: call("GET /count", { n: "x" }) has to be a type error, and the result of call("GET /count", { n: 2 }) has to be a number.

What Check tests

  1. Your code has no type errors
  2. The program prints 42 and Hello, Asha
  3. A wrong request type is rejected
  4. An unknown route is rejected
  5. The count result is a number
  6. The greet result is not a 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.