How to create an array of custom types typescript

问题内容:

I have an interface with the following:

interface orderItem {
  symbol: string,
  side: string,
  price: number
  quantity: number
}

I want to create an array “orders” consisting of “orderItems” in my constructor.

However, orders : [orderItem] doesn’t seem to be doing the trick. Does anyone know how I can proceed?

Here is what I am trying to do in my constructor:

   this.state = {
      orders: orderItem []

  } 

问题评论:

    
Thank you very much for the reply! I tried what you mentioned but it is saying that “orderItem” is referred to as a type but being used as a value.
– blazerix
1 min ago
    
I deleted my comment because your edit now makes it irrelevant. I assumed you were trying to initialize a variable, not create a property in an object.

答案:

答案1:

You need to declare it like this:

class Cart {
  private orders: orderItem[];

  constructor(orders: orderItem[]) {
    this.orders = orders;
  }
}

Typescript documentation for declaring arrays.

答案评论:

答案2:

u put the
down vote
favorite
I have an interface with the following:

interface orderItem {
symbol: string,
side: string,
price: number
quantity: number in the eat

comer verdura

答案评论:

原文地址:

https://stackoverflow.com/questions/47756537/how-to-create-an-array-of-custom-types-typescript

Add a Comment