microsoft / microsoft/TypeScript

Type variable not inferred correctly unless unused declaration is provided

Offen
#52,432 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Type Inference Help Wanted
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

The described bug was noticed during debugging of the declarations provided with the Vue framework.

🔎 Search Terms

generic, inference, function, overload

🕗 Version & Regression Information

All 4.x versions available on the playground.

⏯ Playground Link

Playground link with relevant code

💻 Code
interface Methods {
  [key: string]: Function;
}

interface Param<M extends Methods> {
  data: (this: M) => any;
  methods: M;
}

declare function fun<M extends Methods = {}>(
  param: Param<M> & ThisType<M>
): void;

// Uncommenting the declaration below results in proper type inference of M

// declare function fun<M extends Methods = {}>(
//   param: { unusedProperty: true } & Param<M> & ThisType<M>
// ): void;

fun({
  data() {
    this.myMethod(); // Even after uncommenting the overloaded version, context is not properly inferred here
  },
  methods: {
    myMethod() {},
    mySecondMethod() {
      this.myMethod(); // Error without uncommenting the declaration
    }
  },
});
🙁 Actual behavior
  1. Proper inference of the M type variable and this context in the methods section depends on having an unused overloaded version of a function.
  2. Even though M is properly inferred for fun (visible after hovering over the function) and this context in methods after uncommenting the unused declaration, this context in data is still not properly inferred (when hovering over this, it looks as it is inferred to the default {}).
🙂 Expected behavior

M type variable is properly inferred in both data function and methods section without redundant declarations.

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 damit, das verknüpfte TypeScript Playground-Beispiel auszuführen und die Inferenz für die beiden Deklarationen von fun zu vergleichen, wobei du dich auf das Generic M und die this-Kontexte in data und methods konzentrierst. Da im Issue keine Repository-Datei oder kein Test angegeben ist, ermittle vor dem Hinzufügen eines Regressionstests zunächst die Pfade für Compiler-Inferenz und kontextuelle Typisierung; abgeschlossen ist die Aufgabe, wenn beide Kontexte M ohne die redundante Überladung inferieren.

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
35/100

Neue Issues direkt in Ihr Postfach

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