microsoft / microsoft/TypeScript

TS2769 : Function.bind

Open
#42,700 1 comment 1 reaction 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

🔎 Search Terms

function bind

🕗 Version & Regression Information

TypeScript 4.1 & 4.2-beta

⏯ Playground Link

playground

💻 Code
type Funcs = {
    first (value: string): void;
    second (value: number): void;
}

type Store = {
    commit <Name extends keyof Funcs> (
        name: Name,
        ...args: Parameters<Funcs[Name]>
    ): void;
}

type Func <Name extends keyof Funcs> = (
    ...args: Parameters<Funcs[Name]>
) => void;

declare const store: Store;

function commit<Name extends keyof Funcs> (name: Name): Func<Name> {
    // error
    return store.commit.bind(undefined, name);
}

function commit2<Name extends keyof Funcs> (name: Name): Func<Name> {
    return function (...args: Parameters<Func<Name>>) {
        // correct
        store.commit(name, ...args);
    }
}
🙁 Actual behavior
(method) commit<Name extends keyof Funcs>(name: Name, ...args: Parameters<Funcs[Name]>): void
No overload matches this call.
  Overload 1 of 6, '(this: (this: undefined, arg0: Name, ...args: [value: string] | [value: number]) => void, thisArg: undefined, arg0: Name): (...args: [value: string] | [value: number]) => void', gave the following error.
    The 'this' context of type '<Name extends keyof Funcs>(name: Name, ...args: Parameters<Funcs[Name]>) => void' is not assignable to method's 'this' of type '(this: undefined, arg0: Name, ...args: [value: string] | [value: number]) => void'.
      Types of parameters 'args' and 'args' are incompatible.
        Type '[value: string] | [value: number]' is not assignable to type 'Parameters<Funcs[Name]>'.
          Type '[value: string]' is not assignable to type 'Parameters<Funcs[Name]>'.
  Overload 2 of 6, '(this: (this: undefined, ...args: Name[]) => void, thisArg: undefined, ...args: Name[]): (...args: Name[]) => void', gave the following error.
    The 'this' context of type '<Name extends keyof Funcs>(name: Name, ...args: Parameters<Funcs[Name]>) => void' is not assignable to method's 'this' of type '(this: undefined, ...args: Name[]) => void'.
      Types of parameters 'name' and 'args' are incompatible.
        Type 'Name[]' is not assignable to type '[name: Name, ...args: Parameters<Funcs[Name]>]'.
          Target requires 2 element(s) but source may have fewer.(2769)
🙂 Expected behavior

The function commit works correctly as the function commit2. I'm a bit confused and sorry if this bug has been reported.

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 reproduce the TS2769 error using the generic commit and store.commit.bind example under TypeScript 4.1 or 4.2-beta. No repository file or test is named, so trace the compiler handling of Function.bind and generic parameter tuples; done means commit type-checks with the same behavior as commit2.

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.