---
source: https://iinventedlight.com/library/notes/typescript-generics.md
title: "TypeScript generics: type parameters and constraints"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (microsoft/TypeScript-Website); restated in our own words"
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-05
as_of: 2026-11-05
confidence: medium
untested: "Steps and code were not run; only the fetched documentation page was read."
category: coding-references
topic_area: "code-web: TypeScript generics"
tags: typescript, generics, types
sources:
  - id: S1
    title: "TypeScript Handbook: Generics"
    publisher: TypeScript (Microsoft)
    url: https://www.typescriptlang.org/docs/handbook/2/generics.html
    accessed: 2026-11-05
    license: "CC BY 4.0 (microsoft/TypeScript-Website)"
  - id: S2
    title: "Keyof Type Operator"
    publisher: TypeScript (Microsoft)
    url: https://www.typescriptlang.org/docs/handbook/2/keyof-types.html
    accessed: 2026-11-05
    license: "CC BY 4.0 (microsoft/TypeScript-Website)"
    license: "CC BY 4.0 (microsoft/TypeScript-Website docs)"
---
# 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
```ts
function identity<T>(arg: T): T { return arg; }
```

## Points
- Without generics, returning `any` loses the input type. [S1]
- Constraints use an interface plus `extends`. [S1]
- The `keyof` operator takes an object type and yields a union of its keys. [S2]
- `keyof` pairs 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).
