microsoft / microsoft/TypeScript

'this' not inferred for immediately invoked .bind()/.call()/.apply()

Open
#44,224 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.