1. Concept library
  2. JavaScript & TypeScript
  3. Module 5: Generics in practice

Concept

Generic defaults and inference

A type parameter can have a default, like T = unknown, and TypeScript infers parameters from arguments wherever it can. Understanding where inference comes from tells you when to spell a type out.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain where TypeScript infers a type parameter from and when it gives up
  2. Write a generic type with a default parameter
  3. Spot why an empty array infers never[] and give the parameter explicitly

In practice

A few lines that show Generic defaults and inference doing its job.

TypeScript
type Result<T = void> = { ok: true; value: T } | { ok: false; error: string };

const done: Result = { ok: true, value: undefined };

function wrap<T>(value: T): Result<T> {
  return { ok: true, value };
}
const r = wrap({ id: 1 });        // Result<{ id: number }>
const empty = wrap<string[]>([]); // explicit: [] alone would infer never[]

The common mistake

Watch for

Passing an empty array or object literal and letting inference pick never[] or {}. When there is nothing to infer from, give the type parameter explicitly.

What to know first

Your roadmap puts these before Generic defaults and inference, unless you already know them.

How it stays learned

Once a session shows you understand Generic defaults and inference, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← keyof constraints