microsoft / microsoft/TypeScript

TS2769 : Function.bind

オープン
#42,700 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: This-Typing
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンク先の TypeScript Playground から始め、TypeScript 4.1 または 4.2-beta で、ジェネリックな commit と store.commit.bind の例を使って TS2769 エラーを再現します。リポジトリのファイルもテストも指定されていないため、Function.bind とジェネリックなパラメータタプルをコンパイラが処理する方法を追跡します。commit が commit2 と同じ挙動で型チェックを通れば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。