Chapter 15: TypeScript Basic Generics

TypeScript Basic Generics (aimed at someone who already knows basic types + interfaces + functions)

Imagine we’re sitting together — I’m drawing on a whiteboard, you’re asking questions whenever something feels unclear.

1. The core idea of generics — the one-sentence version

Generics let you write code that works with many different types while still keeping strong type safety.

Without generics you either:

  • write the same logic many times (once for string, once for number, once for User, …)
  • or use any / unknown → lose type safety

With generics you write the logic once and let the caller decide what concrete type should be used.

2. The simplest realistic example everyone meets first

TypeScript
TypeScript

Key moment: When you write <T>, you are declaring a type parameter (like a variable, but for types). The name T is just convention — you can write <Item>, <Element>, <U>, etc.

3. Where do we usually see the <T> ?

Place Syntax example Meaning / When used
Function function identity<T>(arg: T): T Most common starting point
Interface interface Box<T> { content: T } Very frequent — reusable data shapes
Class class Queue<T> { … } Data structures, state holders
Type Alias type Pair<K, V> = { key: K; value: V } Less common than interface, still useful
Arrow function const wrap = <T>(x: T) => [x] Works, but has JSX parsing gotcha (later)

4. Classic real-world examples (copy-paste friendly)

4.1 Identity function (the hello-world of generics)

TypeScript

4.2 First element of array (very common utility)

TypeScript

4.3 Generic interface — very frequent pattern

TypeScript

4.4 Generic class — Queue / Stack example

TypeScript

5. Multiple type parameters (very common after 2–3 months)

TypeScript
TypeScript

6. Very important: Default type parameters

TypeScript

Very common in:

  • React hooks (useState<T>(initial: T))
  • HTTP clients (axios.get<T>(url))

7. Most common “beginner confusion” points (2026 edition)

7.1 Arrow functions + JSX parsing problem

tsx

7.2 When TypeScript cannot infer → you must write <Type>

TypeScript

7.3 const assertions + generics (powerful combo)

TypeScript

Quick reference card — basic generics patterns

Pattern Code example Typical usage area
Generic function function getFirst<T>(arr: T[]): T Utils, helpers
Generic interface interface Result<T> { data: T } API responses, form values
Generic class class List<T> { add(item: T) } Collections, state, caches
Multiple parameters function zip<A,B>(a:A[], b:B[]): [A,B][] Combining different types
Default type type Box<T=string> = { item: T } Optional generic arguments
Generic constraint function longest<T extends { length: number }> Restrict what T can be (very important — next topic)

Next logical steps after basic generics usually are:

  • Constraints (extends)
  • keyof, indexed access types
  • Generic functions with extends + keyof
  • React hooks (useState, useReducer, custom hooks)

Would you like to continue with one of these next?

Or would you prefer:

  • 10 more small realistic examples right now
  • Explanation of constraints (<T extends …>) with examples
  • Common generic utility types from lib (Partial<T>, Pick<T,K>, Record<K,V>, …)
  • Generic React component patterns

Tell me what feels most useful for you at this moment 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *