【问题标题】:Assign Breeze.Entity to a TypeScript defined class将 Breeze.Entity 分配给 TypeScript 定义的类
【发布时间】:2017-07-21 07:03:51
【问题描述】:

不要太沉迷于 Breeze 语法,但“createEntity”正确地返回了一种类型的轻风.Entity。为什么 TypeScript 不允许我将返回的对象分配给 myClass 类型的“实例”,它实现了轻量级实体接口?

这似乎应该可行,但我收到错误“类型'Entity'不可分配给类型'myClass'。类型'Entity'中缺少属性'id'。

我是否完全误解了 TypeScript(或一般而言)中的继承和/或接口?

this.instance = EntityManager.createEntity("myClass", myObj); // instance is defined as type 'myType'  

如果我将 this.instance 定义为“any”,一切正常,但我在其他地方丢失了强类型对象。

我的类和接口...

export class myClass extends base implements Interfaces.IMyClass, breeze.Entity {

    public id: number;
    ....

    constructor() {
        super();
    }
}

export class base implements breeze.Entity {

    constructor() { }

    public entityAspect: breeze.EntityAspect;
    public entityType: breeze.EntityType;
}

export interface IMyClass extends breeze.Entity {

    id: number;
    ...
}

【问题讨论】:

    标签: typescript breeze breeze-sharp


    【解决方案1】:

    我不确定是其他问题还是这个,但我通过使用下面描述的“as”让它工作。

    this.instance = EntityManager.createEntity("myClass", myObj) as MyClass;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2019-07-14
      相关资源
      最近更新 更多