title: "Object is possibly 'undefined'." in TypeScript
body: <p>The code below throws <code>Object is possibly 'undefined'.</code> at <code>obj.field1 += ',' + v;</code>. TypeScript says the <code>obj</code> may be undefined, but the <code>obj</code> can not be undefined at this point because <code>{field1: 'testtest'}</code> is assigned in case <code>map.get(key)</code> returns undefined.</p>
  
  <p>Why I got this error? How can I fix this?</p>
  
  <pre><code>interface TestIF {
    field1: string;
  }
  
  export class MyClass {
    test(): void {
      const map1: Map&lt;string, TestIF&gt; = new Map();
      const map2: Map&lt;string, string&gt; = new Map();
  
      const key = 'mapkey';
      let obj = map1.get(key);
      if (obj == null) {
        obj = {field1: 'testtest'};
        map1.set(key, obj);
      }
  
      map2.forEach( v =&gt; {
        obj.field1 += ',' + v;  // Object is possibly 'undefined'.
      });
    }
  }
  </code></pre>