CodeZettaInterview Hub

TypeScript · Generics · Junior · Concept

Explain generic constraints (extends).

Short Interview Answer

Restrict type parameters so you can access known members safely.

Detailed Explanation

T extends { id: string } enables x.id. Constraints compose carefully with defaults.

Example

function pluck<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}

Did you know this answer?