microsoft / microsoft/TypeScript

`Omit` causes function to lose contextual typing

未关闭
#63,225 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
Needs Investigation
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

implicit any contextual type

### 🕗 Version & Regression Information

- This changed between versions 5.9.3 and 6.0 - rc

### ⏯ Playground Link

[Playground Link](https://www.typescriptlang.org/play/?ts=6.0.0-dev.20260309#code/CYUwxgNghgTiAEEQBd4A8Bc8De8BmArgHZjICWA9kVgM7IxlEDm8AvgLABQIaADhTFTIAnrwQBBGjRCCoAIyQBlZFGQgAPABUAfPAC8OLvGPwA2gAV4jeAGsQwinnib4UGvHMBdLAAoAblAQBCBYmhaeAJT6un4UZMAA3FwcnKCQsAiEJORUrlIyyAD6dKoaLjxqRMDuFHIAVuDI2j4UoQA08CVqWJLSsgogyqVa2hFYsfFJnG59RV0gPmgd2EYmWaSU1PB+0Ths8AD0B9tW7nQMzMkRXDep4NBw+MQbuTMFxSpqhXJQwFrwFRAVRq9UazVazg68ywAHkALZkZDqXoFeRKT5lbQdABE2NG4ziiS4b0EH1K31+i2Wq2M6xyWx2el0uFYh2OMhgAngAFoTmR3GQ4bwIGRSBBhK4iMIrkA)

### 💻 Code

```ts
declare let x: { function: string }
export type AssertableState = {
[P in keyof T as P]: (value: T[P]) => void;
}
declare function assert_state(o: T, state: AssertableState): void;
assert_state(x, {
function: v => { } // v is string
})

declare function assert_state_bad(o: T, state: Omit, "">): void;
assert_state_bad(x, {
function: v => { } // error - v is implictly any
})
```

### 🙁 Actual behavior

The call to `assert_state_bad` fails because `v` is implicitly typed as `any`

### 🙂 Expected behavior

Either should pass as in 5.9.3

### Additional information about the issue

- Works without the `Omit`
- Works without the `as P` in the mapped type. (original mapping was more complicated, but identity mapping seems to trigger it too)

贡献指南

打开贡献指南

调研方向

首先使用 TypeScript 5.9.3 和 6.0-rc 重现链接的 Playground 示例,比较有无 Omit 以及使用 identity mapped type 时的上下文类型推断。跟踪 Omit 包装的 mapped type 的类型检查路径;完成的标准是回调参数被推断为 string,并且不再出现所报告的 implicit-any 失败。

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

评估

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

把新 issue 发到你的邮箱

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