microsoft / microsoft/TypeScript

Parameters<T> and ReturnType<T> limitation

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

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 3.9.2

Search Terms:
Generic Method Extend Parameters ReturnType

Code

export class Test {

    myMethod(): string {
        return "42";
    }

    myMethod2(a: string): string {
        return "42" + a;
    }

    //  Works  ---
    override<M extends keyof this>(method: M, newFunc: this[M]) {
    }

    //  Works  ---
    override2<M extends keyof this>(method: M, newFunc: (...args: Parameters<this["myMethod2"]>) => ReturnType<this["myMethod2"]>) {
    }

    //  Fails  ---
    override3<M extends keyof this>(method: M, newFunc: (...args: Parameters<this[M]>) => ReturnType<this[M]>) {
    }
}

const test = new Test();

test.override("myMethod2", a => "xxx");
test.override2("myMethod2", a => "xxx");
test.override3("myMethod2", a => "xxx");

Expected behavior:
I would expect override3 to compile and be equivalent to override + override2 (when M == "myMethod2")

Actual behavior:
override3 causes syntax error:

Type 'this[M]' does not satisfy the constraint '(...args: any) => any'.
  Type 'this[keyof this]' is not assignable to type '(...args: any) => any'.
    Type 'this[string] | this[number] | this[symbol]' is not assignable to type '(...args: any) => any'.
      Type 'this[string]' is not assignable to type '(...args: any) => any'.

Playground Link:
https://www.typescriptlang.org/play/?ssl=28&ssc=14&pln=28&pc=15#code/MYGwhgzhAEAqCmEAu0DeAod1vQLYE8BZeJACwHsATACgEoAuaZAJwEsA7AczSxz+ZIBXZu2gAiACwAmMQG5e2AL6Y+BYmSpTqYRiw6cGTJGy48+-ISPHSx0ANTQw8vsoXQA9O+wB1cswDWMNAAtKFu5ABu8MxslPAAPITQ8AAeSPDslDD+8PjkAGbQZKwQAHzUuCQUlIyEADTQ7PAA7gBiguzAjMUQANqEALq0ZjiufJ4+foHYocHhUTGscVKJyWkZWdA5eYU95ZUaNdD1jS3tnYzUAHQ3YMycEIwACndgB9EQ8T29YmpVmmIBqVhgBeUrQABKlnYsHwAAcEt9fkR-pQZEDhhgXCocBNoK0wKwQEFZvNorF4ABmVapdKZbK5ApFUglfao2oNJptDpdaDXW73R7QF7MN4kD5fFl9QbA6BgyHQ2EIyUlfoYkZKdCuYDkdjIIqIFAg07NOCGujydDpZBXSLkpbwajI9TVGQNMBy8FiFI+sS0eTWpC2hYUrTO1Fuxye8Q+lJ+gOG4P2uKUp1-Q6Rj3y72+-3oIA

Related Issues:

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground の再現コードから始め、T が this[M] でインデックス付けされたジェネリックの場合に、コンパイラーが Parameters と ReturnType をどのように処理するかを調べてください。override3 の例が myMethod2 に対して期待される推論でコンパイルされ、その動作が適切なコンパイラーテストでカバーされれば、issue は完了です。

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

評価

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

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

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