microsoft / microsoft/TypeScript

Cannot infer `thisArg` argument type of `Function.bind` for functions with a `this` type consisting of generic functions

Open
#43,239 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: This-Typing
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

Consider a function like-

declare function callback<T>(this: CallbackCtx<T>): void;

Where CallbackCtx is an object containing generic function(s)-

interface CallbackCtx<T> {
    onsuccess: (x: T) => number;
    onerror: (err: any) => void;
}

For such a function, that has its this type set to an object containing generic function(s), Function.bind cannot infer the type of thisArg (from the first overload) correctly.

If the type parameter to .bind is explicitly specified (by substituting the generic function type to be a concrete type) - it works fine.

The type of thisArg inferred by typescript automatically, is CallbackCtx<unknown>.

It should be noted that if CallbackCtx contains only non function-like property(s) - the types are inferred correctly. As seen in this playground

🔎 Search Terms

bind, generic, function

🕗 Version & Regression Information

4.2.3

⏯ Playground Link

Playground link with relevant code

💻 Code
interface CallbackCtx<T> {
    onsuccess: (x: T) => number;
    onerror: (err: any) => void;
}

declare function callback<T>(this: CallbackCtx<T>): void;

declare const ctx: CallbackCtx<string>;

callback.bind(ctx);
// ^ CallbackCtx<string> is not assignable to CallbackCtx<unknown>

callback.bind<(this: CallbackCtx<string>) => void>(ctx);
// ^ Works fine when manually substituting the generic type of the function

🙁 Actual behavior

The line callback.bind(ctx); produces errors

🙂 Expected behavior

The line callback.bind(ctx); should infer the type of callback correctly by substituting the generic type with the help of the type of ctx and should not produce errors

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 and the callback.bind(ctx) example, comparing it with the explicitly typed .bind call. Trace how the this type containing generic function properties is inferred for Function.bind. Done means the inferred context uses CallbackCtx<string> from ctx and the unannotated call produces no error.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.