I'm trying to add an extension function to the Array.prototype but only for Arrays of a generic type extending an interface:
const a: MyTypeExtendingInterface[] = [];
a.myFunc(); // Works
const b: number[] = [];
b.myFunc(); // Compiler error
This is what I tried so far, but it doesn't compile. Any hints?
interface MyInterface {
v: number;
}
declare interface Array<T extends MyInterface> {
myFunc(this: Array<T extends MyInterface>): Array<T extends MyInterface>;
}
Object.defineProperty(Array.prototype, 'myFunc', {
value: function(this: Array<T extends MyInterface>): Array<T extends MyInterface> {
return this;
},
});