title: TypeScript function parameter type inferring not working
body: <p>According to <a href="https://www.typescriptlang.org/docs/handbook/functions.html#inferring-the-types" rel="nofollow noreferrer">TypeScript handbook</a>, there is a type inferring in function type declaration.
  But when I introduce generic type into function declaration, the type inferring somehow not working. I have to explicitly write the type. Why the type inferring is not working when there is generic type?</p>
  <pre class="lang-js prettyprint-override"><code>const fn1: (x: number, y: number) =&gt; number = (x, y) =&gt; {
    // working
    return x + y;
  }
  const fn2: &lt;T&gt;(x: number, y: number) =&gt; Promise&lt;T&gt; = &lt;T&gt;(x, y) =&gt; {
    // not working, although ws hints these parameters' type is number
    ...use type T somewhere...
    ...x + y;
    ...
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/F5vnm.png" rel="nofollow noreferrer">error screen shot</a></p>
  <p>Edit:
  Thank you for the comment. But my question is different.</p>
  <pre class="lang-js prettyprint-override"><code>const fn1: (x: number, y: number) =&gt; number = (param1, param2) =&gt; {
    // typescript will infer the type of param1 and param2 is number
    return param1 + param2;
  }
  const fn2: &lt;T&gt;(x: number, y: number) =&gt; number = &lt;T&gt;(param3, param4) =&gt; {
    // I think typescript will infer the type of param3 and param4 is number
    // but with strict flag true (noImplicitAny), there is a compile error says param3 and param4 is type any
    // why adding generic type to function will cause type inferring not working?
    return param3 + param4;
  }
  </code></pre>
  <p>Edit 2:
  Here is the actual code from the project</p>
  <pre class="lang-js prettyprint-override"><code>type Request = &lt;T&gt;(method: Method, url: string, params?: Record&lt;string, unknown&gt;) =&gt; Promise&lt;T&gt;;
  const request: Request = async &lt;T&gt;(
    // because I have declare the parameters type in type Request
    // why I have to declare the type again here for the parameters?
    // if I omit types here, typescript thinks these params are 'type any'
    method: Method, url: string, params: Record&lt;string, unknown&gt; = {},
  ) =&gt; {
    const fetchConfig: RequestInit = {
      method,
      credentials: 'same-origin',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    };
    let targetUrl = url;
  
    switch (method) {
      case Method.Get:
      case Method.Delete:
        targetUrl += qs.stringify(params, { addQueryPrefix: true, arrayFormat: 'comma' });
        break;
      case Method.Post:
        fetchConfig.body = JSON.stringify(params);
        break;
      default:
    }
  
    const resp = await fetch(targetUrl, fetchConfig);
    // I need to use generic type T here
    const respJson = await resp.json() as T;
    if (resp.status &gt;= 200 &amp;&amp; resp.status &lt; 300) {
      return respJson;
    }
    if (resp.status === 401) {
      globalThis.history.replaceState(undefined, '', RouteURL.adminLogin);
    }
    const newError = new Error('Request failed');
    Object.assign(newError, { json: respJson });
    throw newError;
  };
  </code></pre>