For this specific scenario, the Array<T>
interface is being extended in the following manner:
interface BetterArray<T> extends Array<T> {
push(this: BetterArray<T>, value: T): this;
}
Important note - the implementation of Array<T>.push
looks like this
interface Array<T> {
push(...items: T[]): number;
}
Despite this, a compile-time error occurs:
The interface 'BetterArray' extends 'T[]' incorrectly.
The properties of 'push' are not compatible. The type '(this: BetterArray, value: T) => this' cannot be assigned to the type '(...items: T[]) => number'. The type 'this' cannot be assigned to 'number'. The type 'BetterArray' cannot be assigned to 'number'.
Is there a way to specifically instruct TypeScript to override push in my interface (similar to member hiding in C#)?
Note - TypeScript 2.0 is being used
Upon further investigation, it seems that this issue is solely related to return type - essentially, I wish to enforce a new return type through my interface...
interface A {
fn(): number;
}
interface B extends A {
fn(): this;
}
The interface 'B' extends 'A' incorrectly. The properties of 'fn' are not compatible. The type '() => this' cannot be assigned to '() => number'. The type 'this' cannot be assigned to 'number'. The type 'B' cannot be assigned to 'number'.