TypeScript: Control flow analysis for destructured discriminated unions
Very nice!<p>The very, very related thing I found myself wishing I had just today is the ability to destructure discriminated union fields which only exist on one component type of the union.<p>As an example, with the following type system:<p><pre><code>    type Output = OutputExists | OutputDoesntExist;
    interface OutputExists { exists: true, url: string };
    interface OutputDoesntExist { exists: false };
</code></pre>
The ability to destructure like:<p><pre><code>    const f = ({ exists, url }: Output) => {}
</code></pre>
Such that, at the time of destructuring:<p><pre><code>    exists: true | false
    url: string | undefined
</code></pre>
I believe, with this PR, if we re-structured the types above to:<p><pre><code>    interface OutputDoesntExist { exists: false, url: undefined };
</code></pre>
That destructuring would be possible, with all the control flow analysis necessary to make it useful. Today, even that wouldn't be possible, so definitely excited for this change.<p>But, I'm curious if anyone knows if this change can extend to situations where a field isn't even defined in one of the unions, and the compiler can just insert an undefined type for those fields. Or, maybe this would have some unresolveable side-effects I'm not considering.