Typescript 使用Backbone.js中的Collection.model属性
我有以下代码:Typescript 使用Backbone.js中的Collection.model属性,typescript,Typescript,我有以下代码: class Student extends Backbone.Model { // id, lastname, firstname, birthdate } class StudentList extends Backbone.Collection { model = Student; ... } 我总是遇到这样的错误 Type of overridden member 'm
class Student extends Backbone.Model {
// id, lastname, firstname, birthdate
}
class StudentList extends Backbone.Collection {
model = Student;
...
}
我总是遇到这样的错误
Type of overridden member 'model' is not subtype of original member defined by type 'Collection' ...
在一个官方示例中,代码几乎相同。它在那里工作(Todo->TodoList)。我的主骨d.ts中的收藏声明如下
...
model:Model;
...
有什么想法吗?您正在使用
model=Student将varmodel
的值设置为Student
类型代码>
要覆盖变量,您需要model:Student代码>并设置需要的值model:Student=nameofSomeInstanceOffClassStudent代码>(或者只是model=nameOfSomeInstanceOfClassStudent;
)或者,如果您正在用一个新学生初始化它,model:student=new student()代码>我通过在构造函数中指定模型类型解决了这个问题
export class Users extends Backbone.Collection<Models.User> {
constructor(models?: Models.User[], options?: any) {
super(models, options);
this.model = Models.User;
}
}
导出类用户扩展主干。集合{
构造函数(模型?:模型。用户[],选项?:任何){
超级(型号、选项);
this.model=Models.User;
}
}