I want to implement an array-like class that:
- accepts array as constructor's parameter
- should be iterable and have all built-in array's methods
- have some custom methods
- Should be able to extend other class
I see it like this:
class BaseModel {
arr: Array;
constructor(arr: Array<any>) { // <= req. #1
this.arr = arr;
}
serialize(arr) { // <= req. #3
this.arr = arr;
}
}
class ListModel extends BaseModel { // <= req. #4
constructor(arr: Array<any>) { // <= req. #1
super(arr);
}
sayHello() { // <= req. #3
console.log('hello');
}
}
let list = new ListModel([1,2,3]);
list.sayHello();
// expected output:
// 'hello'
list.push(4); // <= req. #2
for (let a of list) { // <= req. #2
console.log(a);
}
// expected output:
// 1
// 2
// 3
// 4
list.serialize([2,3]);
for (let a of list) {
console.log(a);
}
// expected output:
// 2
// 3
Is it possible with typescript? I looked for solution but haven't found something closer to these requirements. Thx!