title: Typescript Promise<MyClass | undefined> not returning optional variable
body: <p>I have a function basically like this</p>
  
  <pre><code>function foo(): Promise&lt;MyClass | undefined&gt; {
      if(somethingGoesWrong) {
          return Promise.resolve(undefined);
      }
      return Promise.resolve(new MyClass());
  }
  </code></pre>
  
  <p>Outside the function, I call this</p>
  
  <pre><code>const x = await foo()
  </code></pre>
  
  <p>The problem is my IDE which is VSCode won't treat x as <code>MyClass | undefined</code>. It will just be <code>MyClass</code>.</p>
  
  <p>I don't know is this an IDE problem or there are rules such as "Promise can't resolve undefined".</p>
  
  <p>I know a better way to do this is probably throw some error instead of return undefined, I just want to know why is this not working or seems not working.</p>