Repository: TypeScript
Issue: Random reports of error 7022 (implicitly has type 'any' because self-reference)
Labels: Needs Investigation

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer (7022)

### 🕗 Version & Regression Information

Tested on playground:

- 4.5.5 works as expected (OK)
- 4.6.4 just can't suggest any properties of the effected variables (ERR)
- 4.7.4 shows the error 7022 on the effected variables (ERR)

### ⏯ Playground Link

[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.8.4#code/JYOwLgpgTgZghgYwgAgLIE8Aq6AOKDeAsAFDLIC2EYAFgPYAmAFOXDgGIgBcyjOcUccgEFujAJTIAvAD5kAZzBRQAcwA0yPgPIAhbgqUhlEmcjgh0Y7gDdawegG4SAXxIkEtEAuSgFZpNwxsPClkEABXABsI0zlTc0diEh8wPwgAOkoaBkZGZSoANTgIsIh1AGsICylZIlJkAHp65AB5EAj0ZGpWPE9vGGQAAwr0Ae9Y+ggECP4IemQPZBoUCNAUOAAjWisIEjJ3TzBkKyKSkLywQuKIcQTdhoAqe7vke+QASX70WjD5CAjJw4DNL-Qw0UZmOZgXAoIEDdRLQb7LzHK6jFYgNabbbIADuwCi8joOOQ0CgtCgaWerw+yC+PwAVmEvHI-gDBiDlGD1BN-pB6OoITEysgYOT5GFlHkFMAPHJ1BBtlB0DQVLjyWVYoyvDBVpS6i93ocinJaGqoBqRas+rTvsgAERQCC+KCHTAAZV+UEVdp4YoAwooIgBqD2Cx0wR1yaiqhE4aboZRksIgegSOAwSBQTpwKwx6goUliqO0PGGZBhHB6sgvep3R1gMJQEBHE7pDk0BJOMQJIA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
interface MyType {
  method(mapFn: (paramA: () => string, paramB: string) => any): void;
}

const instance: MyType = null as any;

instance.method((getValue, key) => {
  // Only happens if `key` is declared on the line above
  const value = getValue();

  /**
   * If you select `.length` and type `.`, the `const value` line above will show error.
   * If you just select `length`, deleted, and ask for suggestions, everything works just fine.
   * It also works fine if you "restart TS server" (or Ctrl+S and refreshing the playground) after having the error showing up.
   */
  return value.length;
});
```

### 🙁 Actual behavior

[Context, skip if you are in a hurry]
On a big project I'm working on, sometimes happens that as I'm developing some variables would start having `unknown` values, and some others would start showing the 7022 error. The solution I always had is to restart the TS server and most of the time that solves it.

Recently I've found a consistent way of triggering this error on something that feels like it's not self-referencing in any way, which I reduced to this snippet. Before posting this I did a search and found many other bugs opened with code that works with loops, arrays, etc. that in one way or another have some self-referencing, but this one I feel it's different.

[Main content of this section]
On some cases TS Server emits 7022 while changing the contents of a file, in cases where there's no self-referencing variables.

On this snippet, the error doesn't show up if you remove the second argument of the function (`key`), or if you select `length`, delete it and ask for suggestions (TS server still understands it's a string and shows a list of its properties).

However, if you select `.length` and press `.`, then it emits 7022 and doesn't know what's the type of `value` (eventhough if you hover over `getValue()` to see its type, you can see it's just `() => string`).

If you restart the TS server at this point (or the playground by saving + reloading), it will show a parse error (because the code is not finished), but the type of `value` will correctly be `string`.

(Note that in my big project, sometimes TS would still show up 7022 errors even when the file was syntactically correct. Only forcing a TS Server reset would fix it)

### 🙂 Expected behavior

TS Server should not emit 7022 when there are no self-referencing variables.