microsoft / microsoft/TypeScript

Intra expression inference doesn't work within reverse mapped types

Offen
#53,018 6 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Experience Enhancement Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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)
Playground link with relevant code (tuple variant)

💻 Code
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). 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den beiden verlinkten Playground-Beispielen und untersuche anschließend src/compiler/checker.ts bei inferReverseMappedType und inferFromIntraExpressionSites, einschließlich der zitierten Inferenzaufrufe und der Behandlung des Kontexts. Fertig ist die Untersuchung, wenn die nicht annotierten Funktionen produce und consume innerhalb des reverse mapped type konsistent mit den funktionierenden Beispielen inferieren und der gemeldete Code einer Typprüfung unterzogen wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.