title: TypeScript: cannot seem to understand this generics usage for promise
body: <p>Here is a function, which takes a promise and a number which is for timeout and returns another promise. If the promise as the first argument doesn't resolve before the timeout, the promise returned from the function will reject immediately. Or it will resolve the value when the first promise resolves.</p>
  <pre><code>function resolveOrTimeout&lt;T&gt;(promise: Promise&lt;T&gt;, timeout: number): Promise&lt;T&gt; {
    return new Promise&lt;T&gt;((resolve, reject) =&gt; {
      // start the timeout, reject when it triggers
      const task = setTimeout(() =&gt; reject(&quot;time up!&quot;), timeout);
  
      promise.then(val =&gt; {
        // cancel the timeout
        clearTimeout(task);
  
        // resolve with the value
        resolve(val);
      });
    });
  }
  resolveOrTimeout(fetch(&quot;&quot;), 3000);
  </code></pre>
  <p>I understand the logic of the function and I am well aware of how promise works in JavaScript. But I just don't understand the type annotations here, specifically the usage of generics here. I understand that generics parameterize types like functions parameterize value, but here why do we need to parameterize the types in the first place? And also why is that even the function call doesn't provide the type variables as the <code>T</code> in the generics, the compiler doesn't report any errors?</p>