← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

TypeScript Generics

TypeScript 2.0 Advanced
debt(d5/e3/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list TypeScript compiler and ESLint as tools. The TypeScript compiler itself will flag cases where generics are misused (e.g., accessing properties on unconstrained T), but the common mistake of using `any` instead of a generic is not a compile error — it requires a specialist lint rule (e.g., @typescript-eslint/no-explicit-any) or deliberate code review to catch. It won't surface as a runtime error since `any` still 'works'.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes adding a type parameter <T> to functions and using extends to constrain — this is a small, localised change within a single function or file. It's more than a trivial one-liner (e1) since it requires updating the function signature and possibly the return type and call sites, but it doesn't span multiple files or components.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to scope covers web and cli contexts broadly, but the debt of misusing generics (using `any` instead) is largely contained within the specific functions or modules where it occurs. It doesn't inherently shape the whole system architecture — it's a per-function productivity tax rather than a codebase-wide structural burden.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field explicitly states that developers believe generics are only for library authors, causing them to reach for `any` instead. This is a well-documented gotcha that most developers encounter — competent devs familiar with other typed languages might expect type inference to handle this automatically without explicit type parameters, leading to the `any` pattern. It's a documented pitfall but not one that contradicts fundamentally how similar concepts work.

About DEBT scoring →

Also Known As

type parameters generic functions generic types

TL;DR

Type parameters that allow functions, classes, and interfaces to work with any type while preserving type information — the TypeScript equivalent of PHP templates or Java generics.

Explanation

Generics enable reusable, type-safe code. function identity<T>(arg: T): T preserves the type relationship between input and output. Constraints (<T extends object>) restrict what types are accepted. Generic interfaces (Array<T>, Promise<T>) and classes make collections type-safe. Conditional types (T extends string ? A : B) and infer enable type-level computation. Without generics, typed collections require either any (unsafe) or dozens of overloads.

Diagram

flowchart LR
    subgraph Without_Generics
        ANY[function identity any: any<br/>loses type information]
        ANY_USE[const x = identity 42<br/>x is type any - unsafe]
    end
    subgraph With_Generics
        GEN[function identity T T: T<br/>preserves type]
        GEN_USE[const x = identity 42<br/>x is type number - safe]
    end
    subgraph Constraints
        CONST[T extends HasLength<br/>restrict what T can be]
        COND[T extends string number<br/>conditional types]
    end
style ANY fill:#f85149,color:#fff
style GEN fill:#238636,color:#fff
style GEN_USE fill:#238636,color:#fff

Common Misconception

Generics are only for library authors — any time you write a reusable function that should preserve type information (first(), groupBy(), fetchJson()), generics are the correct tool.

Why It Matters

Without generics, a typed array utility requires separate implementations for every type — generics express 'this works for any type' while keeping the type relationship safe.

Common Mistakes

  • Using any instead of a generic when the function should preserve type: function first(arr: any[]) vs function first<T>(arr: T[]): T.
  • Over-constraining generics — <T extends string> when <T> would work, unnecessarily limiting callers.
  • Not using the return type from generics — the whole point is that the caller gets back the specific type they passed in.
  • Unconstrained generics accessing properties — T has no known properties; use <T extends {id: number}> if you need id.

Code Examples

✗ Vulnerable
// Loses type information — always returns any:
function first(arr: any[]): any {
    return arr[0];
}
const name = first(['Alice', 'Bob']); // name is 'any' — no type safety
name.toUpperCase();  // OK but also: name.nonExistentMethod() — no error
✓ Fixed
// Generic — preserves type:
function first<T>(arr: T[]): T | undefined {
    return arr[0];
}
const name = first(['Alice', 'Bob']); // name is 'string | undefined'
name?.toUpperCase();   // ✅ string method
name?.nonExistent();   // ❌ TypeScript error — doesn't exist on string

// With constraint:
function getById<T extends { id: number }>(items: T[], id: number): T | undefined {
    return items.find(item => item.id === id);
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 93
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 1 ping W 2 pings T 0 pings F 0 pings S 0 pings S 0 pings M
No pings yet today
No pings yesterday
Scrapy 16 Amazonbot 10 Perplexity 9 Ahrefs 6 SEMrush 5 PetalBot 5 Google 3 ChatGPT 2 Bing 2 Meta AI 1 Twitter/X 1 Applebot 1 Brave Search 1
crawler 60 crawler_json 2
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Generic typescript A generic is a placeholder for a type that gets filled in later, letting you write one function or class that works with many types while still being type-safe.

Generics let you reuse logic across types without resorting to `any`, which is the difference between safe, self-documenting code and code that silently breaks at runtime.

💡 If a function should work with many types but keep type safety, reach for `<T>` instead of `any`.

Ask Codex about Generic →
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Add a type parameter <T> to functions that work with any type while preserving type information — use extends to constrain T to a minimum shape
📦 Applies To
typescript 2.0 web cli
🔗 Prerequisites
🔍 Detection Hints
Function parameter typed as any when a generic would preserve the type through; casting away type information unnecessarily
Auto-detectable: ✓ Yes typescript eslint
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File Tests: Update


✓ schema.org compliant