How to write a more declarative TypeScript Code? Maybe monad implementation