microsoft / microsoft/TypeScript

Type parameter not usable as type argument in identical type; union in constraint becomes intersection

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

还没有人认领这个 Issue。

Bug Domain: Indexed Access Types Help Wanted
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

🔎 Search Terms

indexed access, constraints, generic, unions, ts2344

🕗 Version & Regression Information
  • This changed between versions 3.3 and 3.5 (likely #30769)
⏯ Playground Link

Playground link

💻 Code
interface Foo<T extends { x: string }, U extends (T | { x: "a" })['x']> {
    z: Foo<T, U> // error!
    //        ~ Type 'U' does not satisfy the constraint 'T["x"] & "a"'. 🙃
}
🙁 Actual behavior

The U type parameter is rejected as a type argument with a TS2344 error about how it doesn't satisfy the constraint. The constraint seems to have shifted from a union to an intersection, even though it comes from an identical place.

🙂 Expected behavior

The U type parameter should be accepted as a type argument.

Additional information about the issue

Distilled from SO question.

I expect this is a consequence of #30769 as per https://github.com/microsoft/TypeScript/issues/31731#issuecomment-498465358, but it is at least somewhat surprising that this should happen when the types involved are identical. One could rewrite the constraint to U extends T["x"] | "a", of course, but this is distilled from the above SO question which is presumably distilled from some use case. What's happening here, exactly, and is it intended, a design limitation, or a bona fide bug?

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 TypeScript Playground 以及 issue 中的最小泛型约束示例开始。调查为什么索引访问约束会被视为交集,而不是预期的并集,同时考虑 3.3 和 3.5 版本之间报告的回归,以及相关 issue #30769。完成的标准是确定这是预期行为,或修正该行为,使 U 能够在没有 TS2344 的情况下被接受。

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

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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