microsoft / microsoft/TypeScript

Wrong inference for defaulted generic in nested call

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

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

Bug Domain: check: Type Inference
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 Search Terms

nested generic call inline inference contextual typing function calls default type parameter

🕗 Version & Regression Information
  • I see the same issue from version 4.1.5 to nightly in TypeScript Playground
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.0-dev.20260401#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwCsctUAeAFXhAA8MRVgBneRjGEgcwBp4BVK2vSYs2nHgDUBdBs1btUHeAF54AclUA+ABStYGAFzxyPIYd49GIAA4B+Q+ICUhgAYASAN7kAvh-E-3vF7OAFCgkLAIKOjYeAJW4BgU2gBuUBDIIIbkTvDu8Bg4AEIgWjTxmCDAWTnJxMDwXsHBZQlaRCRaqgASIBAQOKo8qgDqODAQwKoODgB0BcWdPX04o+OTDvAA9JvwAEZQwE1geKzwAO5YGAAWOMgYAMrWsFAFMMqExKiLvf2DaqsTKbBbbwUHwAB6Nma1HKGC0F2utweTxgLzGs3mJQAREt+gDgFiNiCODgcIdobC2p9vss-iMxoChmppnMiiVuj8cPB8VMtjsSWTgkA

💻 Code
declare function join<T extends string, U extends string, V extends string = ''>(start: T, end: U, sep?: V): `${T}${V}${U}`
declare function expect<T>(value: T): { toBe(expected: T): void }

expect(join('Hello', 'World')).toBe('HelloWorld') // bad

const withoutSeparator = join('Hello', 'World')
//    ^? const withoutSeparator: "HelloWorld"
expect(withoutSeparator).toBe("HelloWorld") // good

expect(join('Hello', 'World', ' ')).toBe('Hello World') // good
🙁 Actual behavior

TypeScript does not infer the return types correctly of a nested function call when an optional generic param is omitted, even if a default is provided. When the function call is not nested, it is inferred correctly. It is also inferred correctly when all params are provided.

🙂 Expected behavior

I would expect that both of these approaches would compile:

expect(join('Hello', 'World')).toBe('HelloWorld') // bad

const withoutSeparator = join('Hello', 'World')
expect(withoutSeparator).toBe("HelloWorld") // good
Additional information about the issue

There are very similar issues, such as #54184, #56714, and #62680 but they didn't quite match up to what I'm seeing here, particularly since the reproductions are quite a bit more complex.

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

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

はじめの一歩

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

調査の方向性

まず、リンク先の TypeScript Playground の再現コードを実行し、ネストされた呼び出しを単独の呼び出しおよび完全に指定された呼び出しと比較してください。既存の推論コンテキストについては、関連する issue #54184、#56714、#62680 を読んでください。完了条件は、区切り文字を省略した両方の例が期待されるリテラル型でコンパイルされ、区切り文字を指定したケースが引き続き正しいことです。

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

評価

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

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

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