microsoft / microsoft/TypeScript

[5.9 regression] as const readonly tuple inferred as mutable through a nested generic call under a conditional-type circular constraint

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

还没有人认领这个 Issue。

Bug Domain: check: Type Inference
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

readonly tuple stripped, as const widening, mutable tuple inference, circular constraint, F-bounded constraint, conditional type constraint, contextual inference, 5.9 regression

### 🕗 Version & Regression Information

- **Worked in 5.8.3** (and in nightlies up to and including `5.9.0-dev.20250609`)
- **Broken since `5.9.0-dev.20250610`**, including 5.9.0-beta, 5.9.2, 5.9.3, and nightly `7.1.0-dev.20260720.1`
- Bisected via npm nightlies to the June 9, 2025 merge window. The commits in that window are #61828, #61837, #61668, #61805, #61822; the behavior change is consistent with **#61668** ("Fix type variable leaks and cache inconsistencies"), which made outer type parameters in contextual types instantiate (possibly to their constraints) before inferences are made to return types of inner function calls — matching this repro's requirement of a nested generic call under a constraint-derived contextual type (see below).

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3#code/JYOwLgpgTgZghgYwgAgEIHsAeAeAKgPmQG8AoZZKCOAE3RABsBPZANznoFcIAuZXEgL4kwjAA4oAwnUjhsAJUIBeZHOQRMM6gGc0WbKBjRkAVUIB+E8l4gILaMLEoMmADLAA1hHlLdOKeAhZBXwSEmoIBHo4SmQYDhAEMGA6ZAAjPQIACjZOHj4ASl5nPBCwiKiYuISklJh5NQ1A7V83T298bN45QpVQ8MjolAQ6LTBkOF4icesOAFtUoyF+iqGRsdTJtJn5xdCAej3kQBlyZABWADoATiK9AG0pieQQOYWoZAEAGmItp5fFgF1CIAUAgoVFoDGYoygwFE4mosSg6FmyDAAAsUHAtABaYYgUYojiiegQEgHZCAUHIzucABznADMNxwlBodCYyHu01+Ozen2+Gy5r3egJIuPxUAAjMhlDBMulMJlbnAvql-uMdKKwPl8vtDpSpjkuLxmeC2RzHs9ue8vlN+RbBQJVQJkCCtHBZigDRidHBkNRgJREqTDuhUgArCJjejASBQdjITwQUQ6Y2sxgitYUABMUtimX17EN7KVaVVmOQGve2qDFN82CmKYhT144oA3KCWY2tLwAESo4Dd97A9sm5gAKlEiPEUCSEC0o-GIHhUZj7GrInEOi0HCgLGAdjbqeHh7AhOJWj20VjjG9MTgMEMiQg1HTeLGUDpOZlcrzTeQ4q+XbIAA5H2QHvGq5ZrFqOo1sUDamg82z2taPx2gCQ4gHQWIBtuWh7qsr6xqAYzADoMCgCSyyDLE8SJMkIDIAA5vU6iaDoxTxO4mEAO4gPgHQsF0PRyC+YoACw5oxspYAqxYqhBGrQdWlJwWCh5mkhiwobafw8oCzrlv6CAcBUkGEXAxGsMAPqgOEmBPtWiBIFoOiZJhZl+jUIBxuuED5MgpHjPQWjoLEFFlAMlS0V5yCoixjSLuxehyLcQGekBgICUJXSiW+pw5qi0nyoqyqluqUHakAA

### 💻 Code

```ts
interface Box {
readonly value: T
}
type Content = R extends Box ? U : never
type BoxLike = Box>

declare function box(value: T): Box

declare function f>(v: R): R

declare const a: { a: number }
declare const b: { b: number }

// ❌ 5.9: Box<[{ a: number }, { b: number }]> — readonly stripped from the as-const tuple
// ✅ 5.8.3: Box
const r1 = f(box([a, b] as const))

// ✅ { value: readonly [{ a: number }, { b: number }] } — same value as a direct
// object literal keeps readonly
const r2 = f({ value: [a, b] as const })

// ✅ Box<{ readonly n: 1; readonly s: "hi" }> — readonly *properties* and literal
// types survive; only readonly tuples/arrays are affected
const r3 = f(box({ n: 1, s: 'hi' } as const))

// ✅ Box — non-recursive constraint is fine
declare function g>(v: R): R
const r4 = g(box([a, b] as const))

// ✅ Box — circular constraint via indexed
// access (no conditional type) is also fine
declare function h>(v: R): R
const r5 = h(box([a, b] as const))
```

### 🙁 Actual behavior

In `r1`, the `as const` tuple loses its `readonly` modifier: `R` is inferred as `Box<[{ a: number }, { b: number }]>` (mutable tuple). In 5.8.3 the same code inferred `Box`.

All three ingredients are required to trigger it:

1. the type parameter has a self-referential (F-bounded) constraint that goes through a **conditional type** — `R extends Box>` where `Content = R extends Box ? U : never`;
2. the argument flows through a **nested generic call** (`box(...)`) rather than being a direct literal — `r2` is fine — or a pre-typed variable;
3. the inferred type contains a **readonly tuple**. Readonly object properties and literal types under the same `as const` survive (`r3`).

Removing any one ingredient preserves `readonly`: a closed constraint (`r4`) or an indexed-access circular constraint with no conditional type (`r5`) both infer `Box`.

Ingredients 1 and 2 line up with the change described in #61668: since that PR, the return type of the inner `box(...)` call is checked against a contextual type in which the outer `R` has been instantiated to its constraint — here a conditional-type-based self-referential constraint — and somewhere in that inference path the tuple candidate's `readonly` modifier is dropped.

The same trigger also affects context-sensitive callback arguments (`f2>(body: () => R)` with an inline arrow or generator body), which is how this was originally hit: a Rust-style `Result` library exposing `gen>(body: () => Generator)` inferred `Result<[A, B], E>` from a body that returned `ok([a, b] as const)`.

### 🙂 Expected behavior

`r1` infers `Box`, as it did in 5.8.3 and consistent with `r2`, `r4`, and `r5`, which differ only in details that should not affect the tuple's readonly-ness. A `const` assertion produces a non-widening readonly tuple; inference should not convert it to a mutable one.

Note the failure mode is silent unsoundness rather than an inference error: `r1.value.push(...)` type-checks against a frozen-in-intent tuple.

### Additional information about the issue

Possibly related: #62071 (another inference regression linked to #61668), #51377 (inference failure with `T extends F`), #44821 (contextual inference at call sites with circular constraints).

贡献指南

打开贡献指南

从这里开始

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

调研方向

使用 TypeScript 5.9.3 在链接的 Playground 中重现 `r1` 案例,并将其推断出的类型与 5.8.3 进行比较,同时使用 `r2`、`r4` 和 `r5` 作为对照。阅读与 #61668 相关的行为变化,并跟踪条件循环约束下嵌套 `box` 调用的上下文推断路径;当 `r1` 推断出 `Box` 且不使 `push` 有效时,即为完成。

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

评估

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

把新 issue 发到你的邮箱

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