ie. ie. - 1 year ago 113
TypeScript Question

Defining prototype property in TypeScript

I have a class, let's say

. I need to declare there a prototype property that can be accessible as follows:

var idKey = A.prototype.attributeId;

I can do it using the following code:

class A {
constructor() {
A.prototype.attributeId = "InternalId";

Is there a better way of doing it?

Answer Source

Yes, if you are targeting ECMAScript5 or above then you can use the following property syntax on a class

   class A {
        private internalId: number = 1;
        get attributeId() {
            return this.internalId;
        set attributeId(newVal) {
            this.internalId = newVal;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download