microsoft / microsoft/TypeScript

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

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

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

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

説明

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

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground と callback.bind(ctx) の例から始め、明示的に型付けされた .bind 呼び出しと比較します。ジェネリック関数プロパティを含む this 型が Function.bind に対してどのように推論されるかを追跡します。推論されたコンテキストが ctx から CallbackCtx<string> を使用し、注釈のない呼び出しでエラーが発生しなければ完了です。

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

評価

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

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

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