title: Is it possible to create dynamic getters/setters in typescript?
body: <p>I'm new in typescript, and I'm trying to rewrite our application from es2016 to TypeScript.
  My task is to have a class with data property and make each element from data object available as class property.</p>
  <p>I get stuck on this JavaScript code:</p>
  <pre><code>for(let key in this.data) {
    Object.defineProperty(this, key, {
      get: function(value:any) { return this.data[key]; },
      set: function(value:any) {
        if (this.data[key] !== value) {
          this.data[key] = value;
          this.updatedKeys.push(key);
        }
      },
    });
  }
  </code></pre>
  <p>It is pretty easy to use getter/setters for typescript, but i get confused if i can create them dynamically?</p>
  <pre><code>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}) {
      super();
      this.data = data;
      this.fields = fields;
      
      for(let key in this.data) {
        Object.defineProperty(this, key, {
          get: function(value:any) { return this.data[key]; },
          set: function(value:any) {
            if (this.data[key] !== value) {
              this.data[key] = value;
              this.updatedKeys.push(key);
            }
          },
        });
      }
    }
  }
  </code></pre>
  <p><code>tsc -t ES2016 --lib &quot;es2016&quot;,&quot;dom&quot; models.ts</code></p>
  <p>will give this error:</p>
  <pre><code>models.ts(33,40): error TS2345: Argument of type '{ get: (value: any) =&gt; any; set: (value: any) =&gt; void; }' is not assignable to parameter of type 'PropertyDescriptor &amp; ThisType&lt;any&gt;'.
    Type '{ get: (value: any) =&gt; any; set: (value: any) =&gt; void; }' is not assignable to type 'PropertyDescriptor'.
      Types of property 'get' are incompatible.
        Type '(value: any) =&gt; any' is not assignable to type '() =&gt; any'.
  </code></pre>
  <p>And I don't know how to get rid of this problem.</p>