TypeScript generics
Short answer
Generics let a function or type work over many types while keeping type information. Constraints (extends) limit what type arguments are allowed. [S1]
Sketch
function identity<T>(arg: T): T { return arg; }
Points
- Without generics, returning
anyloses the input type. [S1] - Constraints use an interface plus
extends. [S1] - The
keyofoperator takes an object type and yields a union of its keys. [S2] keyofpairs especially well with mapped types. [S2]
Claims
- id: C1 claim: Generics work over many types rather than one. source: S1 evidence: "work over a variety of types rather than a single one"
- id: C2 claim: Returning any loses the original type information. source: S1 evidence: "we actually are losing the information about what that type was"
- id: C3 claim: Constraints use extends on an interface. source: S1 evidence: "use this interface and the extends keyword to denote our constraint"
- id: C4 claim: keyof produces a literal union of an object type's keys. source: S2 evidence: "produces a string or numeric literal union of its keys"
Sources
- [S1] TypeScript Handbook: Generics. TypeScript (Microsoft). https://www.typescriptlang.org/docs/handbook/2/generics.html . License: CC BY 4.0 (microsoft/TypeScript-Website). Accessed 2026-11-05.
- [S2] Keyof Type Operator. TypeScript (Microsoft). https://www.typescriptlang.org/docs/handbook/2/keyof-types.html . License: CC BY 4.0 (microsoft/TypeScript-Website). Accessed 2026-11-05.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
Source notice: TypeScript Handbook / Website docs © Microsoft. Licensed under Creative Commons Attribution 4.0 International (https://github.com/microsoft/TypeScript-Website/blob/v2/LICENSE).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: TypeScript (Microsoft), TypeScript Handbook: Generics, CC BY 4.0 (microsoft/TypeScript-Website); TypeScript (Microsoft), Keyof Type Operator, CC BY 4.0 (microsoft/TypeScript-Website).
Cite this
iinventedlight. 2026. "TypeScript generics: type parameters and constraints." iinventedlight Library, iil:note:k4g0zfbx, version of 2026-11-04. https://iinventedlight.com/library/notes/typescript-generics/. License: CC-BY-4.0.
@misc{iil_note_k4g0zfbx,
author = {{iinventedlight}},
title = {{TypeScript generics: type parameters and constraints}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/typescript-generics/},
note = {Permanent ID iil:note:k4g0zfbx. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/typescript-generics.md.