how do you declare an array of objects inside typescript?

I think you must declare the class "Product" so you can declare a Product array like this:

products: Product[];

and pass it as a parameter like this:

bagTotal = (products: Product[]) => {
 // function does stuff
}

To have the class you can do a new .ts file with this code:

export class Product {
  name: String;
  price: Number;
  description: String;
}

I wish that helped!

Thank you.


You're almost there, the placement of the brackets is just wrong:

{name: string, price: number, description: string}[]

The way you had it isn't entirely wrong, but it means something else: it means an array with exactly one item of this type.


I'd also recommend extracting it to an interface, it'd make the type reusable and the thing easier to read:

interface Product {
    name: string;
    price: number;
    description: string;
}

const products: Product[];