user1705545 user1705545 - 3 months ago 15
TypeScript Question

Using the Collection.model attribute in Backbone.js

I have following code:

class Student extends Backbone.Model {
// id, lastname, firstname, birthdate
}


class StudentList extends Backbone.Collection {
model = Student;
...
}


I get always an error like this.

Type of overridden member 'model' is not subtype of original member defined by type 'Collection' ...


In an official example is nearly the same code. There it works (Todo->TodoList). The declaration of Collection in my backbone.d.ts is this.

...
model:Model;
...


Any ideas?

Answer

You're setting the value of the var model to the type Student with model = Student;

To override the variable you would need model:Student; and to set the value you would need model:Student = nameOfSomeInstanceOfClassStudent; (or just model = nameOfSomeInstanceOfClassStudent;) or, if you're initializing it with a new student, model:Student = new Student();

Comments