Is there a convenient way to create my own Array subclass where all of the various methods that return an array will return the subclass type instead?
Say I have a custom array type:
class MyArray extends Array {
foo() {
console.log("Pretend it's much more different from Array than this.")
}
}
const bar = new MyArray();
const baz = bar.filter(n => n > 1);
bar.foo();
baz.foo(); // foo does not exist. baz is any[] not MyArray
I want from and filter and map etc. to return MyArray not an Array. The only way I know how to do this is to tediously reimplement every method, where super.method is called and then the result is converted to MyArray and returned.
Is there a more convenient way to do this? Even a semi-convenient way for not having to re-describe every function signature + overloads.