LLearnCode
Academy/typescript/Programming the type system

advanced · 24 minute study

Generics preserve relationships

A reusable function should preserve what it knows about the caller's data instead of returning any or a vague union.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

First, build a mental model

A type parameter is an unknown chosen by the caller that remains consistent within one call. A constraint describes operations you may perform on that unknown.

  1. T captures the caller's specific type.
  2. K extends keyof T restricts keys to properties that exist.
  3. T[K] retrieves the property's type, preserving the input/output relationship.
  4. Use a generic when it connects positions; a needless type parameter creates work without knowledge.
Read the codetypescript · educational example
function pick<T, K extends keyof T>(value: T, key: K): T[K] {
  return value[key];
}
const order = { id: 'demo', quantity: 3, paid: false };
const quantity = pick(order, 'quantity');
const paid = pick(order, 'paid');
// pick(order, 'missing');
  1. Infer T

    T is the specific shape of order for this call.

  2. Constrain K

    The string 'quantity' is a valid key of T.

  3. Return T[K]

    The result is number for quantity and boolean for paid.

Production thinking

Take this beyond the example.

Prefer small generic helpers with concrete call-site inference over elaborate type puzzles. Test empty inputs and runtime boundaries separately.

Sources and further reading

Your evidence

unseen is a local learning signal, not a credential or proof of production competence.

Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.

Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.

Next lesson · Derive contracts without copying fields →