I can extend Array.prototype and use it in javascript like that:
Object.defineProperty(Array.prototype, 'flatten', {
value: function flatten() {
return this.reduce((a, b) => a.concat(b), []);
}
});
// that allows me to do:
[1, 2, [3, 4]].flatten(); // [1,2,3,4]
[1, 2, [3, 4]].map(e => 5).flatten(); // [5, 5, 5]
However under typescript I get an Error: Property 'flatten' does not exist on type 'Array[]'.
I can avoid that doing something like the following, but that is not nice to get a fluent interface...it's harder to read.
(<any>[1, 2, [3, 4]]).flatten()
([1, 2, [3, 4]].map(e => 5) as any).flatten()
Can I do something in typescript to avoid a casting to any? Like let to know typescript dynamically than the code of type Array has another method.