Repository: TypeScript
Issue: Intra expression inference doesn't work within reverse mapped types
Labels: Suggestion,Experience Enhancement

# Bug Report

### 🔎 Search Terms

intra expression inference reverse mapped type

### 🕗 Version & Regression Information

- This is the behavior in every version I tried

### ⏯ Playground Link

[Playground link with relevant code (object variant)](https://www.typescriptlang.org/play?ts=5.1.0-dev.20230227#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVIB4AVAPgAoAoeeWAcwC54BvK6+AbQGl4t8BrEAE8cieEQC6jFmzYAHGDmDIwIRmVSMAzhhi9aASngBeEmK7iA3Kxlg8m5AFtV8MgA9GRc4ZPwAbjixgKxkAX2D4EIp9Rn9AqwB6ePgAdRwYPk0KRDJpGilreHlFZWcyb1MAIgALEAgIHAqAGgLbVHsnNVdy+FcAOgwcABkcAHcQGABhKE0QMubqEOaQ-QSk1PTM7NyofJkipRU1DXhtXVQDY1NUebZW9tKuy57+odHxqZm51kWKZdWT7B1eDAHAgTSoADkGHgIzSfEa8BAvhA+BGWAwVXgFQAkg4isjgPAAGJoTC4fDY1CIcYolQ8fAAeQARgArcAYTQ0VCEgCyIAxik0FRofCg9J0UAAtCBXPIwZpyfTqXB0AgFRgwVkcqwdswCvsSkdutcWnZHA9un0BsMxpNprN9Dcfn8KIkAVggSCwZDobD0gikSiYejMTi8QoCcTSdg8PBKcraQhePBmWzMJyoNz4HyBcAhSKxbwJdLZXBNArY7wE6qTujNVsdbs5AoDs4UOgY-gynqZNQ4BhkDB8NVavUKuEFjdqHdzZ1LS8be97V9J78Vq6ktoPRBgaDwVCYXCA8jUSGsbj8SBCSSO4r4zSa8nU+yM1mc1VBcKoKLxTApTK5XLRUqwfOl1XrbVqF1XJqANFRuxgth+0HYcajqBoJwiKd4BnDoXEeHwrVeW0PgdJ0lnXIA)
[Playground link with relevant code (tuple variant)](https://www.typescriptlang.org/play?ts=5.1.0-dev.20230227#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVIB4AVeEADwxFWAGd40BrVHAd1QG0BdAPgAoAoePFgBzAFzx2gofAB08gN7SZkgNLws+BiACeORPCKcJSlSoAOMHMGRgQE3qgk0MMTSICU8ALzdD7VU4AbmUzMDwaZABbe3heMgkiAM4vX3gANxwsYBCzeABfXJl86U5+DwlM7JCAehr4AHUcGAYafkReKSFTGUtrW1jeVL8AIgALEAgIHBGAGlDw1EiYhzJh+DJZDBwAGVYQGABhKBoQIfmhfPmU2vqmlraOrvgeoT6bOwcneBc3VE8fH5UBcZItloM1oCNltdvsjiczh4QVd+Dd+HUftgpvBgDgQDRUAByDDwFjNBizUjpKikrAYMbwEYASSifWpwHgADE0JhcPgmahEAcqHYNPgAPIAIwAVuAMHQoNR4ABZED06w0EbCBhQMWuKAAWnIlnxND5YqFcHQCDNlEenWkr3g7wGX3WwIWEWiEPWm22exYB2Op3O0hRaIxLiw2Nx+KJJLJLUpIGp+BYdIZzNZVnZXJ52Dw8AFlpFCE08ClsswCqVqvVtC1UB1epghuNcBoZsLmhL1p+dPx7Qd3VCLs+SHz5qGL1CQjgGGQMHw40m0xGRUuIKEYO9q19MIDQYRoc3qI8t0x0YgOLxBOJpPJydTtPpjJZbJAHO56AL-MFwr7ctKzlGsOTrMYNUbZtNH1I0yBNTtzR7ADRVtQcnkdUcrA+REZzyedF2XCYphmDcCi3eAdxWOJITSP1YUDeEQyRMNrnPfggA)

### 💻 Code

```ts
declare function f<T>(
  arg: {
    [K in keyof T]: {
      produce: (n: string) => T[K];
      consume: (x: T[K]) => void;
    };
  }
): void;
// Works
f({
  a: {
    produce: () => "hello",
    consume: (x) => x.toLowerCase(),
  },
});
// Works
f({
  a: {
    produce: (n: string) => n,
    consume: (x) => x.toLowerCase(),
  },
});
// still doesn't work, even with "Improved Function Inference in Objects and Methods" aka intra-expression inference sites
f({
  a: {
    produce: (n) => n,
    consume: (x) => x.toLowerCase(),
  },
});
// still doesn't work, even with "Improved Function Inference in Objects and Methods" aka intra-expression inference sites
f({
  a: {
    produce: function () {
      return "hello";
    },
    consume: (x) => x.toLowerCase(),
  },
});
// still doesn't work, even with "Improved Function Inference in Objects and Methods" aka intra-expression inference sites
f({
  a: {
    produce() {
      return "hello";
    },
    consume: (x) => x.toLowerCase(),
  },
});
```

### 🙁 Actual behavior

This kind of inference that was improved in https://github.com/microsoft/TypeScript/pull/48538 doesn't work within a reverse mapped type.

### 🙂 Expected behavior

It should work in the same way as it does outside of the reverse mapped type. The code in the playground is almost literally taken from the 4.7 announcement blog post about this improvement ([here](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#improved-function-inference-in-objects-and-methods)). The only difference is that the `produce`/`consume` "pair" is within a property of reverse mapped type

---

Part of the problem is likely in that `inferReverseMappedType` calls `inferTypes` with separate `inferences` here:
https://github.dev/microsoft/TypeScript/blob/e2283e99b47942b863d016c65a3e430dca1549b9/src/compiler/checker.ts#L23938-L23939
and that is not linked anyhow to the current inference `context` and `inferFromIntraExpressionSites` tries to infer this kind of stuff from `context.intraExpressionInferenceSites`+`context.inferences` and the latter won't be present for the computed reverse mapped type inference:
https://github.dev/microsoft/TypeScript/blob/e2283e99b47942b863d016c65a3e430dca1549b9/src/compiler/checker.ts#L23756-L23763

However, I also noticed that `inferReverseMappedType` is not called **at all** in this case so those inferences for it aren't even gathered in the first place.