microsoft / microsoft/TypeScript
Wrong inference for defaulted generic in nested call
まだ誰も着手していません。
- 主要言語
- 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
💻 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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、リンク先の TypeScript Playground の再現コードを実行し、ネストされた呼び出しを単独の呼び出しおよび完全に指定された呼び出しと比較してください。既存の推論コンテキストについては、関連する issue #54184、#56714、#62680 を読んでください。完了条件は、区切り文字を省略した両方の例が期待されるリテラル型でコンパイルされ、区切り文字を指定したケースが引き続き正しいことです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100