microsoft / microsoft/TypeScript
Parameter types not being inferred in a class method whose type is indexed from an interface
オープン
まだ誰も着手していません。
Needs Investigation
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
TypeScript Version: 3.7.4
Search Terms:
Index type
Parameter type of method
Inferring parameter types
Code
interface Parameters1 {
paramA: number;
paramB: string;
}
interface Parameters2 {
paramC: boolean;
}
interface SomeFunctions {
foo(options: Parameters1): void;
bar(options: Parameters2): void;
}
class AClass implements SomeFunctions {
public foo(options: Parameters1): void { }
public bar(options: Parameters2): void { }
}
const aClassInstance = new AClass();
function callMethod<K extends keyof SomeFunctions>(
methodName: K,
options: Parameters<SomeFunctions[K]>[0]
) {
// 1. Does not work:
// Argument of type 'Parameters<SomeFunctions[K]>[0]' is not assignable to parameter of type 'Parameters1 & Parameters2'.
aClassInstance[methodName](options);
// 2. Also does not work
// Argument of type 'Parameters<SomeFunctions[K]>[0]' is not assignable to parameter of type 'Parameters1 & Parameters2'.
// Type 'Parameters<SomeFunctions[K]>[0]' is not assignable to type 'Parameters1'.
(aClassInstance[methodName] as SomeFunctions[K])(options);
// 3. Does work, even though it is essentially the same thing as 2
type MethodType = (o: typeof options) => ReturnType<SomeFunctions[K]>
(aClassInstance[methodName] as MethodType)(options);
}
Expected behavior:
Case 2 should work. Possibly also case 1.
Typescript should know that options is the correct type
Actual behavior:
Only case 3 works, even though it is more or less the same as case 2
Playground Link:
Related Issues:
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
TypeScript Playground の再現から始め、callMethod 内の 3 つの呼び出しを比較します。特に、ケース 1 と 2 を動作するケース 3 と比較してください。コンパイラーがケース 2、場合によってはケース 1 も受け入れ、インデックス付きメソッドに対して期待されるパラメーターの関係を維持できれば、Issue は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 38/100