简体   繁体   English

什么是`Array<class> ` 在打字稿中是什么意思?

[英]What does `Array<class>` mean in typescript?

Here is some code that I came across.这是我遇到的一些代码。 Why does it say dogs: Array<Dog> and not dogs: Array[Dog] ?为什么说dogs: Array<Dog>而不是dogs: Array[Dog] Thank you.谢谢你。

class Dog {
  public name: string // leaving out 'public' would work too
}

class PetStore {
  dogs: Array<Dog>

  printAllDogNames(): void
  {
    this.dogs.forEach(dog => {
      console.log(dog.name);
    });
  }
}

It is a generic array, you can read in this link: https://www.typescriptlang.org/docs/handbook/basic-types.html它是一个通用数组,您可以在此链接中阅读: https : //www.typescriptlang.org/docs/handbook/basic-types.html

Array大批

TypeScript, like JavaScript, allows you to work with arrays of values.与 JavaScript 一样,TypeScript 允许您使用值数组。 Array types can be written in one of two ways.数组类型可以用两种方式之一编写。 In the first, you use the type of the elements followed by [] to denote an array of that element type:首先,使用元素类型后跟 [] 来表示该元素类型的数组:

let list: number[] = [1, 2, 3];

The second way uses a generic array type, Array:第二种方式使用泛型数组类型 Array:

let list: Array<number> = [1, 2, 3];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM