Write a Typed Observable Subject

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

The problem

  • easy
  • Code

Write a class Subject<T>, a value stream others can watch. subscribe(listener) adds a listener and returns a function that removes it; next(value) calls every current listener with the value, in the order they subscribed. A listener only gets values sent after it subscribed. Type it so listeners receive a T, next rejects anything that isn't a T, and subscribe returns a () => void.

Your code must pass

  • Sends each value to every listener, in order
  • Stops sending to a listener once it unsubscribes
  • A later listener only gets later values
  • Has no type errors
  • Listeners get values of type T
  • Rejects next with the wrong type
  • subscribe returns an unsubscribe function