array of class typescript code example

Example 1: typescript array of objects

//Define an interface to standardize and reuse your object
interface Product {
    name: string;
    price: number;
    description: string;
}

let pen: Product = {
  name: "Pen",
  price: 1.43,
  description: "Userful for writing"
}

let products: Product[] = [];
products.push(pen);
//...do other products.push(_) to add more objects...
console.log(products);
/* -->
*[
* {
*  name: "Pen",
*  price: 1.43,
*  description: "Userful for writing"
* },
* ...other objects...
*]

Example 2: typescript array of objects

// Create an interface that describes your object
interface Car {
  name: string;
  brand: string;
  price: number;
}

// The variable `cars` below has a type of an array of car objects.
let cars: Car[];

Example 3: typing arrays

// Arrays are variables containing multiple pieces of data.

myArray = ["value1", 2, 3, "insert fourth value here"];

// To retrieve data, you reference them via index numbers.
// KEEP IN MIND that when referencing array values, index numbers
// start at 1, not zero.

myArray[0] = "value1";
myArray[1] = 2;
myArray[3] = "insert fourth value here";