将 Breeze.Entity 分配给定义为 class 的 TypeScript
Assign Breeze.Entity to a TypeScript defined class
不要太拘泥于 Breeze 语法,但 "createEntity" 正确地 returns 一种 breeze.Entity。为什么 TypeScript 不允许我将返回的对象分配给 "instance",它是 myClass 类型,它实现了 breeze.Entity 接口?
这似乎应该可行,但我收到错误消息“类型 'Entity' 不可分配给类型 'myClass'。类型中缺少 属性 'id' 'Entity'.
我是否完全误解了 TypeScript(或一般)中的继承 and/or 接口?
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;
...
}
我不确定这是其他问题还是这个问题,但我通过使用下面描述的 "as" 使其正常工作。
this.instance = EntityManager.createEntity("myClass", myObj) 作为 MyClass;
不要太拘泥于 Breeze 语法,但 "createEntity" 正确地 returns 一种 breeze.Entity。为什么 TypeScript 不允许我将返回的对象分配给 "instance",它是 myClass 类型,它实现了 breeze.Entity 接口?
这似乎应该可行,但我收到错误消息“类型 'Entity' 不可分配给类型 'myClass'。类型中缺少 属性 'id' 'Entity'.
我是否完全误解了 TypeScript(或一般)中的继承 and/or 接口?
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;
...
}
我不确定这是其他问题还是这个问题,但我通过使用下面描述的 "as" 使其正常工作。
this.instance = EntityManager.createEntity("myClass", myObj) 作为 MyClass;