<p dir="auto">Problem:<br>
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:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// before refactoring
function notify(message: string) : void {
    window.postMessage(message); // &lt;-- 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&lt;void, 'cant-send-a-message'&gt; {
    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"><pre class="notranslate"><span class="pl-c">// before refactoring</span>
<span class="pl-k">function</span> <span class="pl-en">notify</span><span class="pl-kos">(</span><span class="pl-s1">message</span>: <span class="pl-smi">string</span><span class="pl-kos">)</span> : <span class="pl-smi"><span class="pl-k">void</span></span> <span class="pl-kos">{</span>
    <span class="pl-smi">window</span><span class="pl-kos">.</span><span class="pl-en">postMessage</span><span class="pl-kos">(</span><span class="pl-s1">message</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// &lt;-- assuming always works</span>
<span class="pl-kos">}</span>

<span class="pl-c">// turns out that window.postMessage crashes when window gets closed</span>
<span class="pl-c">// need a check and a conditional result</span>

<span class="pl-c">// after refactoring</span>
<span class="pl-k">function</span> <span class="pl-en">notify</span><span class="pl-kos">(</span><span class="pl-s1">message</span>: <span class="pl-smi">string</span><span class="pl-kos">)</span> : <span class="pl-smi">Tried</span><span class="pl-kos">&lt;</span><span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">,</span> <span class="pl-s">'cant-send-a-message'</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-smi">window</span><span class="pl-kos">.</span><span class="pl-c1">closed</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-en">failureFrom</span><span class="pl-kos">(</span><span class="pl-s">'cant-send-a-message'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-en">successFrom</span><span class="pl-kos">(</span><span class="pl-smi">window</span><span class="pl-kos">.</span><span class="pl-en">postMessage</span><span class="pl-kos">(</span><span class="pl-s1">message</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-c">// after such change we need to make sure that all places that call `notify` are addressed</span>
<span class="pl-c">// it's easy to overlook those without a help from a compiler</span></pre></div>
<p dir="auto">Workaround:<br>
use a linter rule</p>
<p dir="auto">Solution:<br>
add a flag to the compiler</p>
<p dir="auto">Hello.</p>
<p dir="auto">Quite often while working with <code class="notranslate">ImmutableJS</code> data structures and other immutable libraries people forget that values are immutable:</p>
<p dir="auto">They can accidentally write:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="obj.set('name', 'newName');"><pre class="notranslate"><code class="notranslate">obj.set('name', 'newName');
</code></pre></div>
<p dir="auto">Instead of:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="let newObj = obj.set('name', 'newName');"><pre class="notranslate"><code class="notranslate">let newObj = obj.set('name', 'newName');
</code></pre></div>
<p dir="auto">These errors are hard to discover and they are very annoying. I think that this problem can't be solved by <code class="notranslate">tslint</code>, so I propose to add some sort of <code class="notranslate">"you must use the result"</code> check into the compiler. But right now I have no idea how I want it to be expressed in the language, so I create this issue mainly to start a discussion.</p>
<p dir="auto">Rust lang, for example, solves a similar problem with <code class="notranslate">#[must_use]</code> annotation. This is not exact what we want, but it's a good example and shows that the problem is quite common.</p>