Microsoft/TypeScript
View on GitHubType variable not inferred correctly unless unused declaration is provided
Open
#52,432 opened on Jan 26, 2023
BugDomain: check: Type InferenceHelp Wanted
Description
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.