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

thanks to the, 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[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;

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