Repository: TypeScript
Issue: Incorrect quickinfo for contextually-provided `this` inference
Labels: Bug,Help Wanted,Domain: Quick Info

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

- this
- any
- return
- function argument

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about `this`

### ⏯ Playground Link

https://www.typescriptlang.org/play?target=99#code/PTAEFsEsDtPBDANqATgUwA4oPYBMCuAxmrqFtgEaJrgBQtAZvtIQC6TbSgMA8AKgD4AFAHMAXKCGsAFpADOEuaxQwRASlABeAaD4aA3gF9GQpi3adJB2qFCFOS0AA8toGfIDcoEKBis0iIjyaNByaNzYKM6gctLYAO5yoADk8NAAnskANKAU+Ky+BbHY+IikFOHJSirQIsk2qGis+ChcTl4+fgFBYaHhDJHRxYkpaZk5eQWQRXGl5ZXVqvWGah70PuhI8uyEdnjh8dJo6GQ4VDSg8fBJIfbM-ui4tJ3Q-oHBfTHY4E1waEnDJIycL2FDoNhudIYcLwBgPL4-Nx-UCDEjTVQ5OTYNxHXxyOT4cJhGhJLooKCsfyvehkhjwYi6GgYRDwfz8HT6BqcADCm38uAk-AATMJCEhEBR6QBrCRSWQKXQabS6IVKnR8Jkstl8EVrWycABKIVwxxIsrFgUlhBlkncCr0Wh0ADdsJBcGrGeBmay0Oy1oY1ibCCyTk74FF-F6tWgJBqoz6ePoItghGoJC63aBDAI1pHvf4GgA6Hl8kimZhsDhcVOgTm2Wz2UJTJtpBmaUAAeQoACs0GxC9c5JARNA5fIcnX67ZJSga0YGrYVnr6+hmq1fC2WGgOmAAAZjXcbpRoeCkbAMWvJ1Pp124Lwz6+gDN3rMNFZFw3G024cvmKtWWsFz2Jsj1YVtwnbO0d1AfcMkPGBj1PFELyTAYUzTJ9b3vcNH2fLxjEXVZ6GeMB4kiKVVD2e5jgAWjQJx4Cjf56AYX9K0sOcGkbRwXEg+VoK6d5ejCCIohcQEUkWWpslAa5QHo6E2BIBofGgbAdnCO08RxcJ6PpIpGJhIFcXIc5wD2E1LiOE5gRRaBEHSUBcEgBgGFNLdtJkVlonkUA1IKVcWmgZSlxYisLC4BghXZURZXDcQYmUVQPQdecorYiLJHi6wGwcApeNklARAE15ug+ESBjEmI4hGKokuknI5IUvt+QaQL13abwwEEnoQgqwZxJqpI6pqOpGpuJxFNa0LGHC-8GAAZjHBUpPUQDcpAgqoK6jc3l6z5KqGIbJPqsbZImqblNsdq2hKvbyv6AbqoSYbVpkprJpakLaCAA

### 💻 Code

```ts
// minimal reproduced problem

function f<T>(g: (this: string) => T) {}
f(function () {
  const x = this; // intellisense for x shows 'any', but it should be 'string'
  return x; // intellisense for x shows 'any', but it should be 'string'
});

// realistic code where problem was encountered
// intellisense sometimes shows the correct type after some time or editing, so the issue seems intermittent

interface Template<T> {
  onCreated: <T2>(callback: (this: T) => T2) => Template<T2>;
  onRendered: (callback: (this: T) => void) => Template<T>;
};
declare var template: Template<{ foo(): void }>;
template
  .onCreated(function () {
    const instance = Object.assign(this, {
      bar() {}
    });
    return instance; // `any` instead of { foo(): void; bar(): void; }
  })
  .onRendered(function () {
    const instance = this; // `any` instead of { foo(): void; bar(): void; }
  });


// working counter-examples

f(function () {
  const x = this; // intellisense for x shows 'string', as expected
  // notice this is the exact same as the problem code where the only difference is that x is not returned
});

function f2<T>(g: (arg: string) => T) {}
f2(function (arg) {
  const x = arg; // intellisense for x shows 'string', as expected
  return x; // intellisense for x shows 'string', as expected
});

function f3(this: string) {
  const x = this; // intellisense for x shows 'string', as expected
  return x; // intellisense for x shows 'string', as expected
}
```

### 🙁 Actual behavior

When a function has a function argument with a user-defined `this`, and the implementation of the function argument assigns `this` to a variable and returns the variable, its type is `any` instead of being the user-defined `this` type.

### 🙂 Expected behavior

The variable that `this` is assigned to should get `this`'s type.