microsoft / microsoft/TypeScript
'this' not inferred for immediately invoked .bind()/.call()/.apply()
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Bug Report
🔎 Search Terms
function type inferrence, function apply function, function call function
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference
⏯ Playground Link
Playground link with relevant code
💻 Code
let f2 = function(this: number[]) {
console.log(this)
}.bind([1, 2, 3]);
// ^^ works fine
let f1 = function() {
console.log(this)
// ^^ 'this' implicitly has type 'any' because it does not have a type annotation.
}.bind([1, 2, 3]);
🙁 Actual behavior
Compiler shows an error: "'this' implicitly has type 'any' because it does not have a type annotation."
🙂 Expected behavior
The type of this to be correctly infered from the bind, call or apply call.
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 with the linked TypeScript Playground reproduction and compare the annotated and unannotated functions using immediately invoked bind, call, and apply. Trace the compiler's handling of this inference for these calls; done means the unannotated example infers the bound array type without the implicit-any error while preserving existing behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100