Is it possible to create dynamic getters/setters in typescript?

thanks to the https://github.com/epicgirl1998, she helped me to find the solution. I'll post it here:

the error is that the getter has a value parameter even though getters aren't passed any value

i replaced it with get: function() { return this.data[key]; }, and now the only error is that there's a super call in the class which is only needed if the class extends another class

also, this inside the accessors doesn't refer to the class instance, but using arrow functions for them should fix it

try this:

interface IData {
  id: number;
  [propName: string]: any;
}

class Model  {
  
  protected updatedKeys:string[] = [];
  
  baseUrl:string = null;
  data:IData;
  fields:IData;

  constructor(data:IData={id:null}, fields:IData={id:null}) {
    
    this.data = data;
    this.fields = fields;
    
    for(let key in this.data) {
      Object.defineProperty(this, key, {
        get: () => { return this.data[key]; },
        set: (value:any) => {
          if (this.data[key] !== value) {
            this.data[key] = value;
            this.updatedKeys.push(key);
          }
        },
      });
    }
  }
}