首页 > 解决方案 > 使用对象属性在打字稿中的其他对象上添加属性?

问题描述

我有一个对象,它具有我想从另一种类型扩展所需的属性,例如:

interface FabricObjectWithId extends fabric.Object {
 id: string
}

接口 INeedPropsOfThisObj 没有 id,所以在 MyObj 接口上,我想添加 id 属性但我得到了错误。帮助?

这是我的用例:

private getObjectOnCanvasById = (id: string) =>
    this.handler.canvas
      .getObjects()
      .find((obj:  FabricObjectWithId)  => obj.id === id);

错误信息:

 Types of parameters 'obj' and 'value' are incompatible.
        Type 'Object' is not assignable to type 'FabricObjectWithId'.

标签: typescript

解决方案


推荐阅读