Recently, I discovered how to create extensions in TypeScript:
interface Array<T> {
lastIndex(): number
}
Array.prototype.lastIndex = function (): number { return this.length - 1 }
Now, I want to figure out how to make a getter from it. For example:
interface Array<T> {
get lastIndex(): number
}
Array.prototype.lastIndex = function (): number { return this.length - 1 }
This way, I can easily call it as a getter in my code like someArray.lastIndex.
While searching for a solution, I came across an answer that seems promising but unfortunately the code doesn't compile for generic types. It's also quite cumbersome to write this way, but maybe I'm just asking too much from TypeScript: How to extend a TypeScript class with a get property?