Ch. 2 · TypeScript

TypeScript Generic Constraints and Relationships

TypeScript Generic Constraints and Relationships. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

A generic captures relationships between inputs and outputs. Constraints permit required operations without discarding the caller’s specific type.

Before you start

You should be comfortable with JavaScript values, functions and objects. Use a TypeScript project with strict checking enabled when trying the examples. Separate what the compiler proves from what must still be checked when values arrive at runtime.

The practical goal is to reason through this situation: A function constrained to {id: string} can return the exact subtype it received. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Find the relationship

The caller needs the same record type back, including extra fields. A helper returning only an ID-bearing interface loses that information.

Step 2: Constrain required operations

Use T extends a minimum shape so the implementation can read id. Do not invent fields that the constraint does not guarantee.

Step 3: Preserve the caller’s type

Return T when the actual original record is returned. Constructing a smaller object is not enough to satisfy every possible subtype.

Worked scenario

A function constrained to {id: string} can return the exact subtype it received.

function requireId<T extends { id: string }>(record: T): T {
  if (record.id.length === 0) throw new Error('Missing ID');
  return record;
}
const user = requireId({ id: 'u1', name: 'Ada' });
console.log(user.name); // name remains available
TypeScript

The constraint enables the id check; T preserves name. Returning { id: record.id } would break the promise made to callers with additional required fields.

Common mistake

An unconstrained type parameter does not guarantee properties exist.

Verify the behavior

Pass records with different extra fields and a record missing id. Check inferred return types and the runtime empty-ID path separately.

Interview exercise

Preserve additional fields in a helper.

Answer and reasoning

Use a constrained T and return T; returning only the constraint would lose subtype information.

Continue learning

Compare the scenario with the TypeScript interview questions and test your understanding with the TypeScript MCQs. For terminology and implementation details, consult the reference material.

More in TypeScript

read ✓TypeScript · hard

TypeScript Abstract Classes and Contracts

Share behavior with abstract classes, enforce required members, and decide when an interface or composition is the better contract.

~2 min readread →
read ✓TypeScript · mid

TypeScript Enums vs Union Types

Compare enums with unions of string literals: runtime cost, serialization, exhaustiveness and which one fits application code.

~3 min readread →
esc