Inside a generic function with a remapped type as parameter, elements are wrongfully inferred as {}.
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- typescript
- 領域
- compilers
調査の方向性
リンクされた Playground の再現から始め、string & keyof H を使用するリマップされた型と keyof H バリアントを比較します。ジェネリック関数 foo 内で要素アクセスがどのように推論されるかを追跡します。リマップされたハンドラー要素が {} ではなく () => void、または unknown や any として推論され、報告されたケースがカバーされていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
🔎 Search Terms
remapped
key mapping
generics
Maybe related:
- https://github.com/microsoft/TypeScript/issues/47794
- https://github.com/microsoft/TypeScript/issues/48855
🕗 Version & Regression Information
- tested on playground v6.0.2
⏯ Playground Link
💻 Code
type RenameKeys<H extends Record<string, any>> = {
[K in string&keyof H as `on${Capitalize<K>}`]: () => void
};
function foo<T extends Record<string, any>>(handlers: RenameKeys<T>) {
for(const name in handlers) {
let handler: () => void = handlers[name]!; // nok
// inferred as {} instead of () => void;
// Type 'NonNullable<RenameKeys<T>[Extract<keyof RenameKeys<T>, string>]>' is not assignable to type '() => void'.
// Type '{}' provides no match for the signature '(): void'.(2322)
}
}
🙁 Actual behavior
The type of the elements of the remapped type is {} inside the generic function.
If we return the element, the returned type is correct (cf playground).
🙂 Expected behavior
The type should be either:
() = void(correct)unknownorany: TS is unable to properly infer the type.
Having {} is quite strange/surprising.
Additional information about the issue
There is another issue with [K in string&keyof H] which doesn't manifest when we use [K in keyof H] (cf playground):
type RenameKeys<H extends Record<string, any[]>> = {
[K in string&keyof H]: (...args: H[K]) => void
};
function foo<T extends Record<string, any>>(handlers: RenameKeys<T>) {
for(const name in handlers)
return handlers[name]!;
throw new Error("ok");
}
const x = foo({onFoo: (e: 42) => {}}); // nok : (...args: unknown) => void
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/TypeScript のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
microsoft/TypeScript#64322 · コメント 2 件 · リアクション 1 件 · 担当者 2 名 ·
-
Possible Improvement
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
microsoft/TypeScript#64278 · コメント 1 件 · リアクション 1 件 ·
-
Docs
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
microsoft/TypeScript#64118 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
microsoft/TypeScript#64094 ·
-
Docs
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
microsoft/TypeScript#63959 · コメント 5 件 ·
microsoft/TypeScript の issue をすべて見る
似ている issue
-
optimization optimization:agents-md-curator
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
githubnext/gh-aw-cao#13143 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
blinklabs-io/bursa#904 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · コメント 2 件 ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100