title: Typescript narrowing of keys in objects when passed to function
body: <p>Why does TypeScript not apply type narrowing on members of objects to the object type itself, such that it can be passed to another function that expects a narrowed type? How can this be fixed/circumvented without loosing typesafety?</p>
  
  <p>Minimal example:</p>
  
  <pre class="lang-js prettyprint-override"><code>type A = { key: string | null};
  type B = {key: string};
  
  function func(a: B) {};
  
  const a: A = {key:'abcd'};
  if(typeof(a.key)==='string') {
    a.key // has (narrowed) type 'string'
    func(a); // still does not work
  }
  </code></pre>
  
  <p>The error message is: <code>Types of property 'key' are incompatible. Type 'string | null' is not assignable to type 'string'.</code></p>
  
  <p><a href="http://www.typescriptlang.org/play/#code/C4TwDgpgBAglC8UDeUDWEQC4oGdgCcBLAOwHMoAfKYgVwBs6BfAbgChRIoAhBZdLXARKkWrVgDMaxAMbBCAe2JRJMgBQBDbFwCUyUa2mK8UTbF5J+mAOTqARtIAmV0YXGqOEeW-UA6ftvhAqzwiMitdJFYoEz8MKOUpaQ1tNkYxIA" rel="noreferrer">Playground-Link</a></p>