All academies
All projects

Expense Tracker

easy

Create a personal finance tracker where users log expenses by category and see monthly totals and a category breakdown. You will practice typing enums-as-unions for categories, using Record and reduce with generics for aggregation, and narrowing form input from strings to numbers safely.

Next.jsTypeScriptTailwind CSSRecharts

Architecture

  • UI layer: form and list components plus chart components that only receive already-aggregated, typed data
  • State layer: React context with a useReducer store, where every action is a member of a discriminated union
  • Domain layer: pure aggregation functions (totals per category, per month) built on typed reduce calls
  • Persistence layer: a localStorage adapter with a versioned schema so old data can be migrated or discarded
  • Type layer: shared Expense and Category types imported by the reducer, the UI, and the chart mappers

Folder structure

src/
  app/
    page.tsx
    layout.tsx
  components/
    ExpenseForm.tsx
    ExpenseList.tsx
    CategoryChart.tsx
    MonthlySummary.tsx
  context/
    ExpenseProvider.tsx
  lib/
    aggregate.ts
    storage.ts
  types/
    expense.ts

Domain type definitions

Start every project by defining its types — they become the contract for every component, service, and API call that follows.

export type Category =
  | "food"
  | "transport"
  | "housing"
  | "entertainment"
  | "health"
  | "other";

export interface Expense {
  id: string;
  amount: number;
  category: Category;
  note: string;
  date: string; // ISO date, e.g. "2026-07-01"
}

export type CategoryTotals = Record<Category, number>;

export type ExpenseAction =
  | { type: "add"; expense: Expense }
  | { type: "remove"; id: string }
  | { type: "update"; expense: Expense };

export interface ExpenseState {
  expenses: Expense[];
}

Build steps

  1. 1Define the domain types — the Category union, Expense interface, and the ExpenseAction discriminated union.
  2. 2Implement the reducer with an exhaustive switch over ExpenseAction, using a never check in the default branch.
  3. 3Wrap the app in ExpenseProvider and expose typed useExpenses and useExpenseDispatch hooks.
  4. 4Build ExpenseForm, converting the amount input from string to number with validation before dispatching.
  5. 5Build ExpenseList with delete and edit support, grouping rows by date.
  6. 6Write aggregate.ts — totalsByCategory returning CategoryTotals and totalsByMonth returning a Record keyed by month string.
  7. 7Feed the aggregates into CategoryChart and MonthlySummary, typing the chart datum shape explicitly.
  8. 8Add localStorage persistence with a schema version field and a migration guard on load.