TypeScript Generics
Write a function once and keep the caller's exact type all the way through it.
In this chapter: generic functions · generic interfaces · constraints with extends · inference · when a generic is overkill
💡 The Core Idea
A generic is a relationship between types, not a placeholder for "any type". T[] → T says the element type coming out is the element type that went in. That relationship is the whole value. The caller's concrete type survives the call, so first([1, 2]) is number | undefined rather than any. A type parameter that appears only once in a signature expresses no relationship. It is almost certainly the wrong tool.
How It Works
// ❌ `any` loses the type on the way through
function firstAny(arr: any[]): any {
return arr[0];
}
firstAny([1, 2]).toUpperCase(); // compiles; crashes
// ✅ the generic ties output to input
function first<T>(arr: readonly T[]): T | undefined {
return arr[0];
}
first([1, 2]).toUpperCase(); // ❌ number has no toUpperCase — caughtTwo or more parameters express a relationship between several positions:
function mapArray<T, U>(arr: readonly T[], transform: (item: T) => U): U[] {
return arr.map(transform);
}
const names = mapArray(users, (u) => u.name); // string[] — U inferred from the callbackGeneric interfaces and classes
interface Repository<T> {
findById(id: number): Promise<T | null>;
create(data: Omit<T, 'id'>): Promise<T>;
update(id: number, data: Partial<T>): Promise<T>;
}
class UserRepository implements Repository<User> {} // T is fixed once, for every memberNaming the parameter once at the interface keeps every member consistent. create cannot accidentally take a different entity from findById.
Constraints
extends narrows what a type parameter may be. That is what lets you actually use the value inside:
// without the constraint, `item.name` would not compile
function logName<T extends { name: string }>(item: T): void {
console.log(item.name);
}keyof is the constraint that matters most in day-to-day code, because it makes property access type-safe:
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const user = { id: 1, name: 'Alice' };
getProperty(user, 'name'); // string
getProperty(user, 'nope'); // ❌ not a key of userNote the return type: T[K], an indexed access type, not a union of all value types. That is why getProperty(user, 'id') is number and not number | string.
Defaults
interface FormField<T = string> {
value: T;
touched: boolean;
}
const email: FormField = { value: '', touched: false }; // T = string
const age: FormField<number> = { value: 0, touched: false };Inference
TypeScript infers type arguments from the call, so you rarely write them. Supply one explicitly when there is nothing to infer from, such as a parameter that appears only in the return type:
async function fetchJson<T>(url: string): Promise<T> {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json() as Promise<T>;
}
const user = await fetchJson<User>('/api/users/1'); // nothing in the args reveals TBe clear about what that signature does not do: it does not validate that the body is a User. fetchJson<T> is an assertion dressed as a generic. Pair it with a runtime schema check at any boundary you do not control.
When to Use It
| Scenario | Reach for | Why |
|---|---|---|
| The return type depends on an argument's type | A generic function | The relationship is the point |
| A container or wrapper reused across entities | A generic interface or class | One declaration keeps every member consistent |
| Property access by key | K extends keyof T, returning T[K] | Typos fail; the exact value type comes back |
| Several functions taking the same shape | A plain interface parameter | No relationship to express — a generic adds noise |
| Untyped external data | unknown plus validation | A generic here only asserts, it does not check |
A Result type is the common case worth memorising:
type Result<T, E = Error> = { ok: true; value: T } | { ok: false; error: E };
async function safeAsync<T>(fn: () => Promise<T>): Promise<Result<T>> {
try {
return { ok: true, value: await fn() };
} catch (error: unknown) {
return { ok: false, error: error instanceof Error ? error : new Error(String(error)) };
}
}Common Mistakes
❌ A type parameter used once. It relates nothing to nothing:
function log<T>(value: T): void {} // ❌ identical to (value: unknown): void❌ Constraining with any. <T extends any> constrains nothing. Either leave it unconstrained, or constrain it to the shape you actually use.
❌ Reaching for a generic instead of a union. If the function handles exactly three known types, string | number | Date says so plainly. A generic implies it works for anything.
❌ Treating fetchJson<User>() as validation. The type argument tells the compiler what to assume, and nothing checks the payload. This is the most common way a "fully typed" codebase still throws undefined is not an object in production.
❌ Over-parameterising. Four type parameters on one function means the signature is doing too much. Split it, or accept a concrete type.
⚠️ Generics are erased at compile time. There is no way to branch on
Tat runtime, nonew T(), and notypeof T. Anything that must exist at runtime has to be passed as a value.
🔑 Key Takeaways
- A generic expresses a relationship between types; a parameter used once expresses nothing.
extendsis what makes a type parameter usable inside the function body.K extends keyof TreturningT[K]is the type-safe property-access pattern.- Type arguments are inferred from arguments — write them only where nothing infers.
- A generic return type is an assertion, not validation; check external data at runtime.
Interview Questions
Q: How do you constrain a generic, and why would you need to?
With extends. Without a constraint the compiler knows nothing about T, so you cannot read a property or call a method on it. T extends { id: number } allows item.id; K extends keyof T restricts a key argument to keys that exist.
Q: When is a generic the wrong tool?
When the type parameter appears once, which means it expresses no relationship and unknown would do. When the set of types is small and known, where a union is clearer. And when the real need is runtime validation, because a generic return type only tells the compiler what to assume.
Q: What does function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] buy over (obj: object, key: string): unknown?
Two things. A key that does not exist on the object fails to compile rather than returning undefined at runtime. And the return type is the exact type of that property, so no cast or narrowing is needed at the call site.
What to Read Next
- TypeScript Advanced and Utility Types — the generics the standard library already gives you, and the conditional and mapped types behind them
- Type Guards — validating what a generic only assumes