microsoft / microsoft/TypeScript
implicit 'any' in method with parameter of inferred generic type
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
implicit 'any' method parameter inferred generic type
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference
⏯ Playground Link
💻 Code
type Constructor<T> = {
new (...args: never[]): T;
};
interface GenericParams<T> {
bar: (t: T) => void;
}
type List<T extends readonly {}[]> = {
[K in keyof T]: [Constructor<T[K]>, GenericParams<T[K]>];
};
declare function foo<T extends readonly {}[]>(params: List<[...T]>): void;
class Foo {
test() {}
}
class Bar {}
// Inference fails
foo([
//^?
[Foo, {bar(t) {
}}],
[Bar, {bar(t) {
}}]
])
// Inference works
foo([
[Foo, {bar(t) {
t.test();
}}]
])
interface NumberParams<T> {
bar: (t: number) => void;
}
type List2<T extends readonly {}[]> = {
[K in keyof T]: [Constructor<T[K]>, NumberParams<T[K]>];
};
declare function foo2<T extends readonly {}[]>(params: List2<[...T]>): void;
// Inference works
foo2([
// ^?
[Foo, {bar(t) {
}}],
[Bar, {bar(t) {
}}]
])
🙁 Actual behavior
In the foo example, the parameter t in the associated bar methods are inferred as type any. It seems that TypeScript should be able to infer the actual types of the parameters.
🙂 Expected behavior
t is inferred as type T in each case, i.e. t should be Foo in the Foo case, and Bar in the Bar case.
There are two additional cases provided here: one with a single element in the provided array, and one where the parameters are not generic (number). In both cases, type inference does assign the correct non-any type. It's unclear what exactly the issue is in the first case.
Additional information about the issue
It's possible this is related to #47599, though I don't have enough knowledge of the internals to say whether or not it's the same class of bug. I haven't found another issue reporting this same behaviour.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされている TypeScript Playground の再現から始め、generic な foo の呼び出しと、単一要素および non-generic な foo2 のケースを比較します。mapped な List 型の周辺での型推論の挙動を追跡します。完了条件は、各 bar パラメーターが any ではなく Foo または Bar として推論され、他のケースも引き続き正しいことです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 38/100