Review generated invoice summary

from Type inference
TypeScript 6 advanced 12 min 5 issues to find

Review this generated invoice summarizer before it becomes a public library function.

Parse an untrusted invoice array, validate each record, and return a stable InvoiceSummary for every input without sorting or exposing accidental inferred API changes.

TypeScript
type Invoice = { id: string; cents: number };
type InvoiceSummary = { count: number; totalCents: number };

export function summarize(raw: string) {
  const invoices = JSON.parse(raw) as Invoice[];
  const cents = [];

  for (const invoice of invoices) {
    cents.push(invoice.cents);
  }

  cents.sort((left, right) => left - right);
  const totalCents = cents.reduce((sum, value) => sum + value, 0);

  if (cents.length === 0) {
    return null;
  }

  return { count: cents.length, totalCents };
}

generated code is illustrative, not from any one model

Open in playground
Report an error