Closed
Description
Problem:
Funtional programming needs better support in TypeScript. Values are everything in FP and in TypeScript the expression statements just silently discard them. Not being able to track places where the values are discarded leads to severe bugs. Example:
// before refactoring
function notify(message: string) : void {
window.postMessage(message); // <-- assuming always works
}
// turns out that window.postMessage crashes when window gets closed
// need a check and a conditional result
// after refactoring
function notify(message: string) : Tried<void, 'cant-send-a-message'> {
if (window.closed) {
return failureFrom('cant-send-a-message');
} else {
return successFrom(window.postMessage(message));
}
}
// after such change we need to make sure that all places that call `notify` are addressed
// it's easy to overlook those without a help from a compiler
Workaround:
use a linter rule
Solution:
add a flag to the compiler