Generics let you write reusable, type-safe utilities. Constrain them when needed and provide defaults for ergonomics.
function identity<T>(value: T): T { return value; }function identity<T>(value: T): T {
return value;
}
function first<T>(list: T[]): T | undefined {
return list[0];
}
function prop<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
type ApiResponse<TData = unknown> = {
data: TData;
status: number;
};
const res: ApiResponse<string> = { data: "ok", status: 200 };Understanding Generics is fundamental to mastering TypeScript. Practice with the examples above and experiment with variations to solidify your knowledge.