2017-03-01 54 views
0

不要太拘泥於Breeze語法,但「createEntity」正確地返回一種breeze.Entity類型。爲什麼TypeScript不允許我將返回的對象分配給實例myClass,該實例實現了breeze.Entity接口?將Breeze.Entity分配給TypeScript定義的類

看起來這應該工作,但我得到的錯誤「類型‘實體’不assignmable鍵入‘MyClass的’財產‘身份證’的類型‘實體’缺失。

完全難道我誤解繼承和/或接口的打字稿(或一般)?

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

如果我定義爲this.instance「任何」,一切正常,但我在其他地方失去我的強類型的對象。

我的班和接口...

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; 
    ... 
} 

回答

0

我不確定是否是其他問題,或者這個,但我通過使用下面描述的「as」來得到它的工作。

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

相關問題