microsoft / microsoft/TypeScript

Member-wise checks give incorrect errors for type assertions

未关闭
#55,167 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

In Discussion Needs Investigation Rescheduled Suggestion
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

Playground Link

// No error. ✅
let okay = { id: 123, blah: "extra" } as { id: number };

// Errors. ❌
let waat = [{ id: 123, blah: "extra" }] as { id: number }[];
//                     ~~~~
// Conversion of type '{ id: number; blah: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Type '{ id: number; blah: string; }' is not comparable to type '{ id: number; }'.
//     Object literal may only specify known properties, and 'blah' does not exist in type '{ id: number; }'.

Expected: Both of these type assertions should consistently apply the appropriate relationship check, and both should be free of errors.

Actual: The first assertion is free of errors, while the second has an excess property error.

Similar example I found over at https://github.com/microsoft/TypeScript/pull/55152/files#r1275497478:

let okay = { foo: "" } as { id: 123 };
//         ~~~~~~~~~~~~~~~~~~~~~~~~~~
// Conversion of type '{ foo: string; }' to type '{ id: 123; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Property 'id' is missing in type '{ foo: string; }' but required in type '{ id: 123; }'.

let waat = [{ foo: "" }] as { id: 123 }[];
//            ~~~
// Conversion of type '{ foo: string; }[]' to type '{ id: 123; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
//   Type '{ foo: string; }' is not comparable to type '{ id: 123; }'.
//     Object literal may only specify known properties, and 'foo' does not exist in type '{ id: 123; }'.

贡献指南

打开贡献指南

从这里开始

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

调研方向

从链接的 TypeScript Playground 重现开始,比较两个类型断言,包括 PR #55152 中的类似示例。追踪数组断言所使用的逐成员关系检查,并为报告的情况添加回归测试;完成的标准是两个断言都能一致地避免错误的 excess-property 错误。

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

评估

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

把新 issue 发到你的邮箱

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