title: How to dynamically assign value to class property in TypeScript
body: <p>I would like to dynamically assign multiple properties to MyClass in TypeScript so there is no need to write multiple lines of code for setting class properties. I wrote the following code:</p>
  
  <pre><code>interface IMy
  {
      visible?: boolean;
      text?: string;
  }
  
  class MyClass
  {
      element: JQuery;
  
      assing(o: IMy): void
      {
          for (let [key, value] of Object.entries(o))
          {
              if (typeof value !== "undefined")
                  this[key] = value;
          }
      }
  
      get visible(): boolean
      {
          return this.element.is(":visible");
      }
  
      set visible(visible: boolean)
      {
          this.element.css("display", visible ? "" : "none");
      }
  
      get text(): string
      {
          return this.element.html();
      }
  
      set text(text: string)
      {
          this.element.html(text);
      }
  }
  
  let t = new MyClass();
  t.assign({ visible: true });
  //instead t.visible = true;
  
  //or
  
  t.assign({ text: "Something" });
  //instead t.text = "something";
  
  //or
  
  t.assign({ text: "Something", visible: false });
  //instead t.visible = true; 
  //        t.text = "something";
  </code></pre>
  
  <p>but, I have problems in line <code>this[key] = value;</code> with error: </p>
  
  <blockquote>
    <p>Index signature of object type implicitly has an 'any' type.</p>
  </blockquote>
  
  <p>What I need to change or my approach is completely wrong?<br>
  It would also be a good solution for setting default properties in constructor when interface is constructor parameter.</p>
  
  <p><strong>Edited:</strong><br>
  About entries I really liked them so I am using this code:</p>
  
  <pre><code>//extensions.d.ts
  interface Object
  {
      entries&lt;T&gt;(o: any): [string, T][];
      entries(o: any): [string, any][];
  }
  //exntesion.js !!note js not ts
  if (!Object.entries)
  {
      Object.entries = function* entries(obj)
      {
          for (let key of Object.keys(obj))
          {
              yield [key, obj[key]];
          }
      };
  }
  </code></pre>
  
  <p><strong>Edited 2:</strong></p>
  
  <p>The primary idea was to solve problems for overloading constructor where you could set only desired properties.<br>
  Here is the complete code if someone else finds it usable.</p>
  
  <pre><code>interface IMy
  {
      prop1?: boolean;
      prop2?: string;
      prop3?: string;
      prop4?: number;
  }
  
  class MyClass implements IMy
  {
      prop1: boolean = false;
      prop2: string = "";
      prop3: string = "Something";
      prop4: number = 0;
  
      constructor(properties: IMy)
      {
          this.assing(properties);
      }
  
      assing(o: IMy): void
      {
          let that = (&lt;any&gt;this);
          for (let key in o)
          {
              if (o.hasOwnProperty(key))
              {
                  let value = (&lt;any&gt;o)[key];
                  if (typeof value !== "undefined" &amp;&amp; typeof that[key] !== "undefined")
                      that[key] = value;
              }
          }
      }
  
  }
  
  let my1 = new MyClass({ prop1: true });
  let my2 = new MyClass({ prop2: "SomeText", prop3: "Anything" });
  //I set prop2 and prop3
  let my3 = new MyClass({ prop4: 10 });    
  //I set prop4 (fourth parameter)
  
  console.log(my1, my2, my3);
  </code></pre>