【问题标题】:How to instantiate, initialize and populate an array in TypeScript?如何在 TypeScript 中实例化、初始化和填充数组?
【发布时间】:2013-01-18 19:09:42
【问题描述】:

我在 TypeScript 中有以下类:

class bar {
    length: number;
}

class foo {
    bars: bar[] = new Array();
}

然后我有:

var ham = new foo();
ham.bars = [
    new bar() {          // <-- compiler says Expected "]" and Expected ";"
        length = 1
    }
];

有没有办法在 TypeScript 中做到这一点?

更新

我想出了另一个解决方案,方法是设置一个返回自身的方法:

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;
    }
}

所以你可以如下初始化它:

var ham = new foo();
ham.setBars(
    [
        new bar(1).height(2),
        new bar(3)
    ]);

【问题讨论】:

  • 在 TypeScript 中使用类似于 C# 的对象初始化器会非常有用。 [ {length: 1} ] 不是 bar 的实例,但如果支持,new bar() { length = 1 } 将是 bar 的实例。也许我们应该为此提出功能建议?

标签: javascript typescript


【解决方案1】:

没有像 JavaScript 或 TypeScript 中的对象那样的字段初始化语法。

选项 1:

class bar {
    // Makes a public field called 'length'
    constructor(public length: number) { }
}

bars = [ new bar(1) ];

选项 2:

interface bar {
    length: number;
}

bars = [ {length: 1} ];

【讨论】:

  • 让事情更清晰和类型安全:bars:bar[]=[{length:1}]
  • 有没有办法在不定义类的情况下做到这一点?
  • 问题是关于如何在一个类中初始化一个数组。如果不使用类,就无法在类中初始化数组。
【解决方案2】:

如果您真的想拥有命名参数并让您的对象成为您的类的实例,您可以执行以下操作:

class bar {
    constructor (options?: {length: number; height: number;}) {
        if (options) {
            this.length = options.length;
            this.height = options.height;
        }
    }
    length: number;
    height: number;
}

class foo {
    bars: bar[] = new Array();
}

var ham = new foo();
ham.bars = [
    new bar({length: 4, height: 2}),
    new bar({length: 1, height: 3})
];

还有here 是打字稿问题跟踪器上的相关项目。

【讨论】:

  • +1 用于问题链接。您还可以使初始化器值可选:options? : {length?: number; height?: number;}
【解决方案3】:

一个简单的解决方案可能是:

interface bar {
    length: number;
}

let bars: bar[];
bars = [];

【讨论】:

    【解决方案4】:

    另一种解决方案:

    interface bar {
        length: number;
    }
    
    bars = [{
      length: 1
    } as bar];
    

    【讨论】:

      【解决方案5】:

      如果您想向页面“添加”其他项目,您可能需要创建一个地图数组。这就是我创建地图数组然后向其中添加结果的方式:

      import { Product } from '../models/product';
      
      products: Array<Product>;          // Initialize the array.
      
      [...]
      
      let i = 0;
      this.service.products( i , (result) => {
      
          if ( i == 0 ) {
              // Create the first element of the array.
              this.products = Array(result);
          } else { 
              // Add to the array of maps.
              this.products.push(result);
          }
      
      });
      

      product.ts 看起来像...

      export class Product {
          id: number;
          [...]
      }
      

      【讨论】:

      • 如果你像这样初始化数组:products: Array&lt;Product&gt; = []; 你不必做 if 子句。
      猜你喜欢
      • 2018-01-31
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多