microsoft / microsoft/TypeScript
Type variable not inferred correctly unless unused declaration is provided
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked TypeScript Playground example and compare inference for the two declarations of fun, focusing on the generic M and the this contexts in data and methods. No repository file or test is identified in the issue, so locate the compiler inference and contextual-typing paths before adding a regression case; done means both contexts infer M without the redundant overload.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100