title: How to instantiate, initialize and populate an array in TypeScript?
body: <p>I have the following classes in TypeScript:</p>
  
  <pre><code>class bar {
      length: number;
  }
  
  class foo {
      bars: bar[] = new Array();
  }
  </code></pre>
  
  <p>And then I have:</p>
  
  <pre><code>var ham = new foo();
  ham.bars = [
      new bar() {          // &lt;-- compiler says Expected "]" and Expected ";"
          length = 1
      }
  ];
  </code></pre>
  
  <p>Is there a way to do that in TypeScript?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I came up with another solution by having a set method to return itself:</p>
  
  <pre><code>class bar {
      length: number;
  
      private ht: number;
      height(h: number): bar {
          this.ht = h; return this;
      }
  
      constructor(len: number) {
          this.length = len;
      }
  }
  
  class foo {
      bars: bar[] = new Array();
      setBars(items: bar[]) {
          this.bars = items;
          return this;
      }
  }
  </code></pre>
  
  <p>so you can initialize it as below:</p>
  
  <pre><code>var ham = new foo();
  ham.setBars(
      [
          new bar(1).height(2),
          new bar(3)
      ]);
  </code></pre>