RUN

Interfaces and objects in TypeScript

Describe the shape of an object and use it in a function. Press Run to try it, then change the code and run it again.

main.ts

interface Product {
  name: string;
  price: number;
  inStock?: boolean;
}

const cart: Product[] = [
  { name: 'Keyboard', price: 1499, inStock: true },
  { name: 'Mouse', price: 599 },
];

const total = cart.reduce((sum, p) => sum + p.price, 0);
console.log(`Total: ₹${total}`);
console.table(cart);
Output

Press Run to see the output.

More TypeScript examples

Open the full TypeScript online compiler →