0

So I learned how to make extension in typescript:

interface Array<T> {
  lastIndex(): number
}

Array.prototype.lastIndex = function (): number { return this.length - 1 }

But How to make a getter from it ? eg:

interface Array<T> {
  get lastIndex(): number
}

Array.prototype.lastIndex = function (): number { return this.length - 1 }

So that I can just call is as getter in code someArray.lastIndex ?

I found this answer but code wont compile for generic types, also it's quite ugly to write this way but well maybe i ask for too much in typescript : How to extend a typescript class with a get property?

Renetik
  • 5,887
  • 1
  • 47
  • 66
  • Possible duplicate of [Is it possible to use getters/setters in TypeScript Interfaces?](https://stackoverflow.com/questions/12844893/is-it-possible-to-use-getters-setters-in-typescript-interfaces) – Raja Jaganathan Jun 02 '19 at 04:21

2 Answers2

2

As far as the typescript interface is concerned a getter is an implementation detail. You can declare it as a normal readonly property and implement it as a getter.

interface Array<T> {
  readonly lastIndex: number
}

Object.defineProperty(Array.prototype, "lastIndex", {
  get: function () { return this.length - 1  }
});

Using ES6 shorthand syntax,

Object.defineProperty(Array.prototype, "lastIndex", {
  get() { return this.length - 1  }
});
Avin Kavish
  • 8,317
  • 1
  • 21
  • 36
  • 1
    This is the correct answer. However you might want to note that augmenting globals should be avoided – Aluan Haddad Jun 02 '19 at 05:25
  • Why is that? sounds like your opinion – Avin Kavish Jun 02 '19 at 05:26
  • Sure, it's an opinion, but it is widely held for good reason. all you have to do is consider what would happen if one of your dependencies did the very same thing to see where the problem occurs. Such code may well conflict with future additions to the language itself. If you're writing an application then it's a questionable decision, if you're writing a library then it's an absolutely awful decision. Also, I voted for your answer for opinionated reasons as well. – Aluan Haddad Jun 02 '19 at 05:30
  • Yeah library authors have no right to modify globals as libraries are meant for 3rd party consumption. But as authors of application code, I see no problem in modifying the global scope as part of application bootstrap. Do you really think there is going to be a language addition called `array.lastIndex` that does something else other than get the last index of the array? Adding a global is an intelligent executive decision to be made only by the CTO/lead in a way that affects the entire codebase. Not every time a developer wants a utility method. – Avin Kavish Jun 02 '19 at 05:54
  • 1
    That's fair. But I do think it is bad for longevity. – Aluan Haddad Jun 02 '19 at 06:11
-2

How about like this:

interface Array<T> {
    lastIndex(): number;
    lastValue(): T
}

Array.prototype.lastIndex = function (): number { return this.length - 1 };
Array.prototype.lastValue = function (): any { return this[this.lastIndex()] };

let myArray = ['111,', '43242asd', 'asdasdas'];
console.log(myArray.lastValue());

Shadab Faiz
  • 2,380
  • 1
  • 18
  • 28