# Multiple Generics & Defaults

> Using multiple type variables and setting default fallback types.

Subject: TypeScript | Lesson 25 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/multiple-generics-defaults
Previous: [Generic Constraints](https://codewithanimation.in/typescript/generic-constraints.md) | Next: [Const Type Parameters](https://codewithanimation.in/typescript/const-type-parameters.md)

## More than one T

A generic signature isn't limited to a single `<T>`. You can declare as many generic variables as you need by separating them with commas: `<T, U, V>`.

Just like function parameters can have default values (`function fn(x = 10)`), generic variables can have default types (`<T = string>`).

## Mapping and Fallbacks

Multiple generics are required when an operation transforms one type into a completely different type (like mapping an array of strings into an array of numbers).

Default generics are useful for building flexible libraries where a type is optional, providing a fallback so the user doesn't have to constantly type `<string>`.

## Syntax

```typescript
// 1. Multiple Generics
function tuple<T, U>(first: T, second: U): [T, U] {
  return [first, second];
}
const pair = tuple("Alice", 42); // [string, number]

// 2. Default Generics
// If the caller doesn't provide a type, T defaults to Error.
interface Result<T = Error> {
  success: boolean;
  payload?: T;
}

// payload is Error
const res1: Result = { success: false, payload: new Error() }; 

// payload is string
const res2: Result<string> = { success: true, payload: "Done" };
```

## Common mistake: All or Nothing

If a function has two generics `<T, U>`, and the compiler cannot infer them, you cannot just manually pass `<U>` and let `<T>` be inferred.

If you manually pass *any* generic arguments to a function, you must pass *all* of them (unless they have defaults). You cannot mix manual overriding and implicit inference in the same function call.

## Tip: Naming conventions

While `T, U, V` is common, it's often better to use descriptive names for complex signatures, such as `<TInput, TOutput>`.

## Try it: Try it

Toggle whether the caller explicitly provides a type argument to see how the default generic fallback `T = string` engages.

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

## Check yourself

### Question 1

What happens if you use `type Config<T = string> = { key: T }` and write `const c: Config = { key: 42 }`?

- A. It compiles correctly because defaults are flexible.
- B. TypeScript throws an error because 'number' is not assignable to 'string'.
- C. The default type is ignored and inferred as a number.

Correct answer: B

Because you didn't provide a type argument, T defaulted to 'string'. Therefore, the object must have a string key, so passing 42 is an error.

- Why not A: Defaults are rigid fallbacks. If it defaults to string, it expects a string.
- Why not C: Generic types on interfaces/types do not infer from assignments the way function arguments do.

## Remember this

- Separate multiple generic parameters with commas: `<T, U>`.
- Use `=` to assign a default fallback type: `<T = string>`.
- Default generics make passing type arguments optional for the caller.
- If you manually provide one generic argument to a function, you must provide all of them (that don't have defaults).
