title: specify the return type without an intermediary variable
body: <p>Sometimes typescript can't infer the return type of a lambda or a method, and I want to specify it by hand to gain type safety.</p>
  
  <p>The problem occurs for instance when using promises or a bit more sophisticated callbacks (for instance with the <code>parsimmon</code> parsing library).</p>
  
  <p>I did not find a way to specify the type of the return variable to get type checking except by creating a local variable:</p>
  
  <pre><code>interface ITest {
      x: number;
      y: string;
  }
  
  // type checks, i'd prefer it doesn't
  function f1() {
      return {x:5} as ITest;
  }
  
  // type checks, i'd prefer it doesn't
  // (I understand it's a cast, just to show also that doesn't help)
  function f2() {
      return &lt;ITest&gt;{x:5};
  }
  
  // correctly fails to type-check
  function f3() {
      const x: ITest = {x:5};
      return x;
  }
  </code></pre>
  
  <p>is there another way to get type checks without creating a local variable? I'm aware of course that I could type <code>function f4(): ITest</code> but my use-cases look more like this:</p>
  
  <pre><code>return P.string(" for ")
      .then(P.takeWhile(c =&gt; c !== ' ').skip(P.string(" in ")))
      .chain(value =&gt; P.takeWhile(c =&gt; c !== ' ')
             .chain(array =&gt; parseNgOptionsTrackBy().atMost(1)
                    .map(trackBy =&gt; {
                        // variable created solely for type-checking
                        const r: NgOptionsData = {
                            select: expressions.select,
                            label: expressions.label,
                            value: value,
                            array: array,
                            trackexpr: trackBy ? trackBy[0] : undefined
                        };
                        return r;
                    })));
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>return new Promise((resolve, reject) =&gt; {
      // some code, then variable created solely for type-checking
      const result: ViewInfo = {
          fileName: fileName,
          ngModuleName: ngModuleName,
          controllerName: controllerName,
          controllerViewInfos: viewInfos};
      resolve(result);
  });
  </code></pre>
  
  <p>In other words I'm deep in some lambda and specifying a return type is not that simple.</p>
  
  <p><strong>UPDATE</strong> I see there is some skepticism that typescript is actually unable to infer <code>resolve</code> for promises. Luckily my app is OSS. I made a commit demonstrating the problem in some branch:
  <a href="https://github.com/emmanueltouzery/ng-typeview/tree/so_question" rel="nofollow noreferrer">https://github.com/emmanueltouzery/ng-typeview/tree/so_question</a></p>
  
  <p>Check <a href="https://github.com/emmanueltouzery/ng-typeview/commit/6a2cee89f4170da9f7a970716b1e84e060296e53" rel="nofollow noreferrer">the last commit on that branch</a>. As you can see, I removed one field from the structure i'm filling (I want a build error!!) but it builds just fine. And I <em>did</em> specify the return type of the function, <code>Promise&lt;ControllerScopeInfo&gt;</code>.
  <a href="https://github.com/emmanueltouzery/ng-typeview/blob/6a2cee89f4170da9f7a970716b1e84e060296e53/src/controller-parser.ts#L248-L253" rel="nofollow noreferrer">This is type-unsafe</a>.
  If you want to build, you must run <code>npm install</code> and then <code>tsc</code>. Though presumably you can just inspect the source and the tsconfig and trust me on the building or not.</p>