microsoft / microsoft/TypeScript

strictBindCallApply error mentions `this` type mismatch, but elaboration points to parameters, even when `this` type matches

Open
#57,225 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Help Wanted Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

strictBindCallApply this arg error

🕗 Version & Regression Information
  • This changed between versions 4.4 and 4.5
⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.5.5#code/KYDwDg9gTgLgBDAnmYcDCBDANlgRhgYwGsBBOAXjgAoMoBzARgC44BnGKASwDs6AaOLToAmFtwCuAW1zAoASgoA+OBOmyA3AChQkWAmSpMOfMQBCFakOZsOPOgvLLVMqFs08YsgGaFUAMXFuAhhOCG5WOABvTThYwRYaemt2Ll4BAmw8QiIWIyziEgcnKRctOLhcBKsWFLt0zJMc9Abs0yKVEo1NAF9NTQATYAIsWlQCMPY4L0Dg0PCWAKCQibcdaHhppbm4AihgDE8AHgAVOFBPbn6IomBECC84RdmJxSobxBZjuQWZ5fCAbWOAF0ojE4nsYOIoNxLEkarY0jtKs1jNkyAAfFH5UjtTbPAHvIEAOgyOCogUGXh4wH6Ais6Vwch6fUGw1GcCwwHgGG4iCePy2E3+73uj1+c1YQJZQxGex2E3gBGReUaGKxqrcfR5fJmJMy5MuwCp3BpAgARGaGUy+kA

💻 Code
export type CallbackA = (arg1: string, arg2: number) => number;
export type CallbackB = (arg1: string) => number;

interface Functions {
    a: (arg1: string, callback: CallbackA) => number;
    b: (arg1: string, callback: CallbackB) => number;
}

declare const functions: Functions;

export function create<T extends keyof Functions>(key: T): Functions[T] {
    return (arg1: string, cb: CallbackA | CallbackA) => functions[key].call(undefined, arg1, cb)
}

declare let anyFunc: Functions[keyof Functions]

declare const cb: CallbackA | CallbackA;



anyFunc.call(undefined, "", cb)

🙁 Actual behavior

The error message says:

The 'this' context of type '((arg1: string, callback: CallbackA) => number) | ((arg1: string, callback: CallbackB) => number)' is not assignable to method's 'this' of type '(this: undefined, args_0: string, args_1: CallbackA) => number'.
  Type '(arg1: string, callback: CallbackB) => number' is not assignable to type '(this: undefined, args_0: string, args_1: CallbackA) => number'.
    Types of parameters 'callback' and 'args_1' are incompatible.
      Type 'CallbackA' is not assignable to type 'CallbackB'.
        Target signature provides too few arguments. Expected 2 or more, but got 1.(2684)

Or:

The 'this' context of type '((arg1: string, callback: CallbackA) => number) | ((arg1: string, callback: CallbackB) => number)' is not assignable to method's 'this' of type '(this: undefined, args_0: string, args_1: CallbackA) => number'.
  Type '(arg1: string, callback: CallbackB) => number' is not assignable to type '(this: undefined, args_0: string, args_1: CallbackA) => number'.(2684)

In neither example, this is not the problem.

In fact, if you add this: void to interface Functions and then cast to void, you get this:

The 'this' context of type '((this: void, arg1: string, callback: CallbackA) => number) | ((this: void, arg1: string, callback: CallbackB) => number)' is not assignable to method's 'this' of type '(this: void, args_0: string, args_1: CallbackA) => number'.
  Type '(this: void, arg1: string, callback: CallbackB) => number' is not assignable to type '(this: void, args_0: string, args_1: CallbackA) => number'.
    Types of parameters 'callback' and 'args_1' are incompatible.
      Type 'CallbackA' is not assignable to type 'CallbackB'.(2684)

Or even undefined:

The 'this' context of type '((this: undefined, arg1: string, callback: CallbackA) => number) | ((this: undefined, arg1: string, callback: CallbackB) => number)' is not assignable to method's 'this' of type '(this: undefined, args_0: string, args_1: CallbackA) => number'.
  Type '(this: undefined, arg1: string, callback: CallbackB) => number' is not assignable to type '(this: undefined, args_0: string, args_1: CallbackA) => number'.(2684)

None of these makes sense.

🙂 Expected behavior

The message doesn't claim that this is the problem; the problem is in other params.

If you go back to 4.4, you get a more reasonable error that simply complains about the callback param:

Argument of type 'CallbackA' is not assignable to parameter of type 'CallbackB'.(2345)
Additional information about the issue

Noticed this while attempting to enable strictBindCallApply on our repo.

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

Reproduce the strictBindCallApply diagnostic from the linked TypeScript Playground, comparing versions 4.4 and 4.5. Trace the diagnostic for the calls involving Functions[keyof Functions] and verify that the final message identifies the incompatible callback parameter rather than blaming the this context.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.