microsoft / microsoft/TypeScript

Wrong inference for defaulted generic in nested call

未关闭
#63,380 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Domain: check: Type Inference
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先运行链接的 TypeScript Playground 复现,并将嵌套调用与独立调用和完整指定的调用进行比较。阅读相关 issue #54184、#56714 和 #62680,了解现有的推断上下文。当两个省略分隔符的示例都能以预期的字面量类型编译,同时提供分隔符的情况仍然正确时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。