Build a Typed Deferred Promise

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Sometimes the code that settles a promise isn't the code that creates it. Export a type Deferred<T>, an object with promise (a Promise<T>), resolve (takes a T) and reject (takes any reason, typed unknown), and write deferred<T>(), which returns one: calling resolve or reject settles the promise, and only the first call counts. The types must make resolve accept only a T.

Your code must pass

  • Resolves the promise with resolve's value
  • Rejects the promise with reject's reason
  • Settles only once
  • Has no type errors
  • promise has T's type
  • resolve only takes a T
  • Deferred has the right shape