It might be the late hour or my brain overloaded with programming, but I'm completely puzzled by this simple class:
export class Path extends Array {
constructor(...params:Array<any>) {
super(...Object.assign([], arguments));
}
until (node) {
let pos = this.findIndex(element => node === element);
return pos < 0 ? [] : this.splice(0, pos + 1);
}
get last () {
return this.length ? this[this.length - 1] : undefined;
}
}
Although the class initializes fine when called in TypeScript, none of the methods seem to work:
path = new Path(1,2,3,4); // [1,2,3,4]
path.last; // undefined
path.until(3); // undefined
Interestingly, when I create a similar class without extending Array, everything works as expected:
export class Bla {
values = [];
constructor(...params:Array<any>) {
this.values = new Array(...Object.assign([], arguments));
}
until (node) {
let pos = this.values.findIndex(element => node === element);
return pos < 0 ? [] : this.values.splice(0, pos + 1);
}
get last () {
return this.values.length ? this.values[this.values.length - 1] : undefined;
}
}
This yields the correct results:
bla = new Bla(1,2,3,4); // {values: [1,2,3,4]}
bla.last; // 4
bla.until(3) // [1,2,3]
To make things even more confusing, if I try the Path class code in the Chrome dev console (excluding the TypeScript typings and export), it works flawlessly:
path = new Path(1,2,3,4); // [1,2,3,4]
path.last; // 4
path.until(3) // [1,2,3]
What am I missing in the enigmatic world of TypeScript?