# Generic Constraints

> Setting minimum requirements for a generic type.

Subject: TypeScript | Lesson 24 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/generic-constraints
Previous: [Basic Generics](https://codewithanimation.in/typescript/basic-generics.md) | Next: [Multiple Generics & Defaults](https://codewithanimation.in/typescript/multiple-generics-defaults.md)

## The 'extends' keyword in Generics

By default, a generic `<T>` can be literally anything—a string, a number, a boolean, or an object.

If you try to access a property on `T` (like `T.length`), TypeScript will throw an error because it cannot guarantee that property exists. You have to constrain the generic using the `extends` keyword to set a minimum requirement.

## Preserving the original type

Why not just write `function logLength(arg: { length: number })`? If you do that, and you pass a `string[]` to the function, the function's return type would become `{ length: number }`. You lose the fact that it was an array!

By writing `function logLength<T extends { length: number }>(arg: T): T`, you guarantee the argument has a length property, BUT you return the exact original `string[]` type.

## Syntax

```typescript
// T MUST be an object with an 'id' string property.
function addTimestamp<T extends { id: string }>(obj: T) {
  return { ...obj, timestamp: Date.now() };
}

// OK: This object has an 'id' string, plus extra properties.
// The return type perfectly preserves the 'name' property!
const user = addTimestamp({ id: "123", name: "Alice" });

// Error: Type 'number' does not satisfy the constraint '{ id: string }'.
// addTimestamp(42);
```

## Common mistake: Confusing 'extends' meaning

In generic constraints, `extends` does NOT mean "inherits from" like it does in class-oriented programming.

In TypeScript generics, `extends` means "is assignable to" or "satisfies the shape of". It acts as a structural gatekeeper.

## Tip: Primitive Constraints

You can constrain generics to primitive types too! e.g., `<T extends string | number>` ensures the caller can only pass a string or a number, but perfectly preserves the exact literal type they pass.

## Try it: Try it

Attempt to pass different types into the constrained generic function. Watch how the type checker acts as a gatekeeper based on the `extends` requirement.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/generic-constraints

## Check yourself

### Question 1

If you call `function echo<T extends { name: string }>(arg: T): T` and pass `{ name: "Bob", age: 30 }`, what is the return type?

- A. { name: string }
- B. { name: string, age: number }
- C. any

Correct answer: B

The constraint `<T extends { name: string }>` just acts as a bouncer checking IDs. Once you're in the club, your exact, original type (including 'age') is preserved by the generic `T`.

- Why not A: That would happen if you didn't use a generic and just typed the parameter directly.
- Why not C: Generics prevent 'any' fallback.

## Remember this

- Unconstrained generics `<T>` cannot have properties accessed safely.
- Use `<T extends Type>` to enforce a minimum requirement on the generic.
- Unlike directly typing the parameter, generic constraints preserve the exact original type of the argument (including extra properties).
