Skip to content

类型 'X' 有否 属性 in 常见使用类型 'Y'

错误信息

                类型有 否属性 in 常见使用类型
              

快速摘要

你 assigned or compared two 对象类型那个有 否属性 in 常见. 这个是 通常 sign 的 using 错误类型.

为什么发生

  • Assigning 对象的 类型到 变量的 类型 B 哪里 and B share 否属性

  • Using 错误接口 for 对象

  • Confusing two 相似但 不同类型

  • 结构输入 revealing incompatible shapes

最小示例

✗ 错误代码
interface User { name: string; }
interface Product { title: string; }
const product: Product = { title: "Widget" };
const user: User = product; // no common properties
✓ 修复代码
interface User { name: string; }
const user: User = { name: "Widget" }; // correct type

Use 正确类型相反的 assigning incompatible 对象.

如何诊断

  • Compare 属性的 两者都类型 in 错误消息

  • 检查如果你're using 正确接口/类型

  • 验证对象是 supposed 到是 assignable 到目标类型

如何修复

  • Use 正确类型 for 对象

  • 添加常见属性如果类型应该是 compatible

  • Use 类型 assertion 如果 assignment 是 intentional

  • 创建 adapter/transform 函数到 convert 之间类型

如何预防

  • Name 类型清晰地到 avoid confusion

  • Use distinct 属性 names for unrelated 类型

相关资源

← 返回语言