title: TypeScript: type narrowing: assert not undefined and hence exclude from type
body: <p>Both of these lines complain with error "Object possibly undefined" on <code>_e[k]</code>:</p>
  
  <pre class="lang-js prettyprint-override"><code>obj[k] = typeof _e[k] === "undefined" ? "" : _e[k].toString();
  obj[k] = _e[k] === undefined ? "" : _e[k].toString();
  </code></pre>
  
  <p><code>_e[k]</code> is a union of many types, therefore a manual type cast is not applicable. How do I get rid of the error?</p>
  
  <p><strong>update</strong>:
  So in a more general manner my question can be phrased as: is there a way to narrow the type of a variable by checking against specific types which should be excluded?</p>
  
  <p>minimal example:</p>
  
  <pre class="lang-js prettyprint-override"><code>class test {
      a!: string;
      b?: number;
  }
  const t: test = { a: "hi", b: 2 };
  for (const k of Object.keys(t) as Array&lt;keyof test&gt;) {
      console.log(t[k] == undefined ? "LOL" : t[k].toString());
  }
  </code></pre>