title: Why Typescript prioritizing type inferring over generic type with default?
body: <p>Do you have an explanation why TS doesn't infer the <code>string</code> type in the first example?
  Despite I used string as a default for the generic <code>T</code> in the <code>bar</code> function?
  Thanks </p>
  <p>The error (in example 1):</p>
  <blockquote>
  <p>Argument of type 'string | number' is not assignable to parameter of type 'string | undefined'.
  Type 'number' is not assignable to type 'string | undefined'.ts(2345)</p>
  </blockquote>
  <pre><code>// Example 1 (has error)
  
  const baz = (a: unknown) =&gt; a;
  
  const bar = &lt;T extends string | number = string&gt;(): T =&gt; {
    return baz('some_value') as T;
  };
  
  const foo = (str?: string) =&gt; `${str} is string`;
  
  // Error!!
  
  foo(bar());
  </code></pre>
  <p>I've two options for a solution but I'm not sure why they work:</p>
  <ul>
  <li>extracting the <code>bar()</code> call into constant (example 2)</li>
  <li>removing the options operator from <code>str</code> (example 3)</li>
  </ul>
  <pre><code>// Example 2 (no error)
  
  const baz = (a: unknown) =&gt; a;
  
  const bar = &lt;T extends string | number = string&gt;(): T =&gt; {
    return baz('some_value') as T;
  };
  
  const foo = (str?: string) =&gt; `${str} is string`;
  
  const barResult = bar();
  foo(barResult);
  </code></pre>
  <p>Remove the <code>?</code>:</p>
  <pre><code>// Example 3 (no error)
  
  const baz = (a: unknown) =&gt; a;
  
  const bar = &lt;T extends string | number = string&gt;(): T =&gt; {
    return baz('some_value') as T;
  };
  
  const foo = (str: string) =&gt; `${str} is string`;
  
  foo(bar());
  </code></pre>