microsoft / microsoft/TypeScript

Type argument with a subset of the parameter constraint's optional keys incorrectly reported as unassignable to constraint

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

还没有人认领这个 Issue。

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

描述

### 🔎 Search Terms

"does not satisfy the constraint" "keyof" "optional"

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "constraint"

### ⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAsiAq4IB54BooFUoQB7AgDsATAZygG8oBtAaSgEtCoBrCEAewDMp4BdAPwAuKAFdCLQhwDuzAL4A+KAF4sAbgCwAKG0BjADYBDUuTgBhIydRKK2qFABuh-aIgBCEXESRUGKnUZmNk4eeCgAMihSYAAnJgBzQRFxSRl5BU0tOW0gA

### 💻 Code

```ts
type MyType = U;

class MyClass {
value!: MyType;
}
```

### 🙁 Actual behavior

The following error is reported:

```
Type '{ [K in keyof T & string]?: unknown; }' does not satisfy the constraint '{ [K in keyof T]?: unknown; }'.
Type 'keyof T' is not assignable to type 'keyof T & string'.
Type 'string | number | symbol' is not assignable to type 'keyof T & string'.
Type 'string' is not assignable to type 'keyof T & string'.
Type 'string' is not assignable to type 'keyof T'.
Type 'keyof T' is not assignable to type 'string'.
Type 'string | number | symbol' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
```

### 🙂 Expected behavior

No error should be reported because due to the keys being optional, `{ [K in keyof T & string]?: unknown }` is a subtype of `{ [K in keyof T]?: unknown }` for all `T`.

### Additional information about the issue

_No response_

贡献指南

打开贡献指南

从这里开始

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

调研方向

重现链接的 TypeScript Playground 示例中的诊断,然后跟踪对具有可选属性和 intersected keyof keys 的映射类型进行泛型约束检查的过程。当该示例能够无错误地编译,同时保留对相关情况的正确约束检查时,这项工作就完成了。

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

评估

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

把新 issue 发到你的邮箱

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