ie. ie. - 4 months ago 53
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?


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;