Real World Example

Cart Rows and CSV Export

Admin dashboards (Flipkart Seller Hub, Shopify) export reports as CSV. Each row is a fixed shape, which fits a tuple perfectly, and the column headers never change, which fits a readonly array.

Example

type Row = [sku: string, product: string, qty: number, price: number];

const HEADERS: readonly string[] = ["SKU", "Product", "Qty", "Price", "Amount"];

const rows: Row[] = [
  ["SKU-101", "USB Cable", 2, 199],
  ["SKU-205", "Phone Cover", 1, 349],
  ["SKU-310", "Earphones", 3, 899],
];

const lines = rows.map(([sku, product, qty, price]) =>
  [sku, product, qty, price, qty * price].join(","));

console.log([HEADERS.join(","), ...lines].join("\n"));
const grand = rows.reduce((sum, [, , qty, price]) => sum + qty * price, 0);
console.log("Grand total:", grand);

Output

SKU,Product,Qty,Price,Amount
SKU-101,USB Cable,2,199,398
SKU-205,Phone Cover,1,349,349
SKU-310,Earphones,3,899,2697
Grand total: 3444