0Pricing
TypeScript Academy · Lesson

Reusable patterns for data models

Apply generic patterns to real models: Paginated , Result , DTOs with Pick/Omit/Partial, and maps keyed by id.

Reusable patterns for data models is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Goal: Package common data shapes as generic types so you reuse structure without losing type safety.

  • Paginated<T>
  • Result<T,E>
  • DTOs with Pick/Omit/Partial
  • Maps keyed by id

Paginated<T>

Paginated<T> centralizes metadata and keeps item type T precise for lists.

type Paginated<T> = {
  items: T[];
  total: number;
  page: number;
  pageSize: number;
};

type User = { id: number; name: string };

const page1: Paginated<User> = {
  items: [{ id: 1, name: "Ada" }],
  total: 10,
  page: 1,
  pageSize: 5,
};

console.log(page1.items[0].name);

Result<T,E>

Result<T,E> models success/failure explicitly with a discriminant (ok) for safe branching.

type Ok<T> = { ok: true; data: T };
type Err<E = string> = { ok: false; error: E };

type Result<T, E = string> = Ok<T> | Err<E>;

function readUser(id: number): Result<{ id: number; name: string }> {
  return id > 0 ? { ok: true, data: { id, name: "Ada" } } : { ok: false, error: "not found" };
}

const r = readUser(1);
if (r.ok) {
  console.log(r.data.name);
} else {
  console.error(r.error);
}

DTO patterns

With Pick/Omit/Partial you derive concise DTO types from your core model—no duplication.

type User = { id: number; name: string; email?: string; role: "user" | "admin" };

type UserCreateDto = Pick<User, "name" | "email">; // fields required to create

// Update = partial editable fields
type UserUpdateDto = Partial<Pick<User, "name" | "email">>;

const createBody: UserCreateDto = { name: "Ada", email: "a@x.io" };
const updateBody: UserUpdateDto = { name: "Ada L." };

MapById<T>

MapById<T> indexes entities by id for fast lookup. Constrain T to ensure an id exists.

type Key = string | number;

type MapById<T extends { id: Key }> = Record<Key, T>;

const byId: MapById<{ id: number; name: string }> = {
  1: { id: 1, name: "Ada" },
  2: { id: 2, name: "Lin" },
};

console.log(byId[1].name);

Tips

Tips:

  • Keep patterns small and composable.
  • Prefer discriminated unions for state machines.
  • Derive DTOs from models—avoid manual copies.

Paginated<T> check

Quick check: Why define a generic Paginated<T>?

Recap

Recap: Generalize recurring shapes: Paginated<T>, Result<T,E>, DTOs with Pick/Omit/Partial, and id-indexed maps. Small helpers scale across features.

Frequently asked questions

Is the “Reusable patterns for data models” lesson free?

Yes — the full text of “Reusable patterns for data models” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reusable patterns for data models”?

Apply generic patterns to real models: Paginated , Result , DTOs with Pick/Omit/Partial, and maps keyed by id. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Reusable patterns for data models” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Generic interfaces & type aliases
  2. Conditional Types (intro) & distribution over unions
  3. Reusable patterns for data models
← Back to TypeScript Academy