microsoft / microsoft/TypeScript
Type variable not inferred correctly unless unused declaration is provided
Dieses Issue hat noch niemand übernommen.
- 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
- Proper inference of the
Mtype variable andthiscontext in themethodssection depends on having an unused overloaded version of a function. - Even though
Mis properly inferred forfun(visible after hovering over the function) andthiscontext inmethodsafter uncommenting the unused declaration,thiscontext indatais still not properly inferred (when hovering overthis, 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
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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