Repository: TypeScript
Issue: Inferring type parameter in arrow function / function expression fails
Labels: Needs Investigation,Rescheduled

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

<!--
  What search terms did you use when trying to find an existing bug report?
  List them here so people in the future can find this one more easily.
-->
inferred type parameter arrow function expression

### 🕗 Version & Regression Information

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->
- I tried every version between 3.3.3 and 4.4.2 in the playground, and I got the same results

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwxAGcMBGAHgDF8QAPQ1YI+cgYT0PoD4AKWAc1IAueO1ScMASngBeLvADaYiQF1eiVCOqSRAJRAZkMVABUAngAcQVVFwDcAWABQoSLAQp02PAWIYATDbwdAxMLMohvALCohwh0nLwAN7wANYgZiIR9PAAvuqa8Np6BkamltbU9s7OAPS18ADyqfBY+OZWAMpgMFgWGPAALAB0I-7OhCSkPHww-P4J8goC-iqSjk51DQCiMDA4MK3tFd29-fAAzMNXF-AAtEOjw-4b9fAmnf4A7KQAnCIdBAAcmyGCBrWYqBwAygRCIWH4qCgACMIAgMDgCBV4CC4vQgcN3gB3THALCIRAgODiLFWZhErAYAAWBCZWEhUAAtgg6OyMAAaeDI5ADZkZeDueBoODQQjAYZbeBK3HiELgsA4ZAQYBChBtEhQcRYKBy+AM5kS-CwZGMmCwMy0hBEtlgFkarU65EIaUgWUgHUYnGggkTPz+GYrRbwHgpdKZCVzfx5STrGpON67faHNrvE49PoDG7Xe6PMavBoffwXQYAVhEAEE5shuTScIhHTi2HiMFFE1lu1HYxl+6r6HY8uD2fAoTC4Qikaj0ZiLLAuQYqfA2x2gV3Rz3ZoIRxJB2lh7E9+PciH0w0lawoBAIPB4YiTUYEHBDMZHcwgUP46Cl7goaOp-qeAHdkBCZ6ugOCciu2CLgqN5KqhJhMuiFTMFuQJxsBcBHBq8Emlgi66hgRIgCA+BikQmF0shbyoWh2IqhIk6QtCEpzq+ZGBhgrHBoSJgkvAZIUlS1GilhZqMiyzJTki3LBLQfKCsKooYQ6ko+n68qKsxzFsWq8DutqupHAaRomv6skWoaCY2hgdowA6AlWGaLpupq5lelKqAyjZAaYsZ+LIZMAQRomJ6fmUyTgSIKx5MmGwReGniYLgqAHgs8Wxd+-6JYmeSXqmThAA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
declare function test1<Fn extends <Context>(arg1: Context) => [Context]>(fn: Fn): ReturnType<Fn>;
declare function test2<Fn extends <Context>(arg1: Context) => { key: Context }>(fn: Fn): ReturnType<Fn>;

// Ok in TypeScript 4.4.2
test1((arg2) => [arg2]);

// Error in TypeScript 3.3.3 - 4.4.2;
// TS2719: Type 'Context' is not assignable to type 'Context'. Two different types with this name exist, but they are unrelated.
//   'Context' could be instantiated with an arbitrary type which could be unrelated to 'Context'.
test2((arg2) => ({ key: arg2 }));

// Error in TypeScript 3.3.3 - 4.4.2;
// TS2345: Argument of type '<Context>(arg2: Context) => { key: Context; }' is not assignable to parameter of type '<Context>(arg1: Context) => { key: Context; }'.
//   Call signature return types '{ key: Context; }' and '{ key: Context; }' are incompatible.
//     The types of 'key' are incompatible between these types.
//       Type 'Context' is not assignable to type 'Context'. Two different types with this name exist, but they are unrelated.
//         'Context' could be instantiated with an arbitrary type which could be unrelated to 'Context'.
test2((arg2) => { return { key: arg2 } });
test2(function(arg2) { return { key: arg2 }; });
```

### 🙁 Actual behavior

<!-- What happened, and why it was wrong -->
There's a TS2719 or TS2345 error indicating that Context could be instantiated with an arbitrary, unrelated type.

### 🙂 Expected behavior

<!-- What you expected to happen instead, and why -->
It should be possible to infer the type of 'Context' since it must be the same in these cases.