DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. TypeScript Generics: From Basics to Advanced Patterns
XLinkedInReddit
MediumFrontend Engineering

TypeScript Generics: From Basics to Advanced Patterns

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • Why Generics?
  • Constraints
  • Advanced Patterns
  • Conditional Types
  • Mapped Types
  • Template Literal Types
  • Common Mistakes

Generics are TypeScript's most powerful feature. At Google, we use them everywhere — from API clients to state management to utility types.

Why Generics?

Generics let you write reusable code that maintains type safety. Without them, you'd need to write separate functions for each type or use any.

// Without generics
function first(arr: any[]): any { return arr[0]; }

// With generics
function first<T>(arr: T[]): T { return arr[0]; }
first([1, 2, 3]); // TypeScript infers T = number

Constraints

interface HasLength { length: number; }

function logLength<T extends HasLength>(item: T): T {
  console.log(item.length);
  return item;
}
logLength("hello"); // OK
logLength([1, 2]); // OK
logLength(42); // Error: number has no length

Advanced Patterns

Conditional Types

type IsString<T> = T extends string ? true : false;
type A = IsString<string>; // true
type B = IsString<number>; // false

// Practical: Extract promise value
type Unwrap<T> = T extends Promise<infer U> ? U : T;
type X = Unwrap<Promise<string>>; // string

Mapped Types

type Readonly<T> = { readonly [K in keyof T]: T[K] };
type Optional<T> = { [K in keyof T]?: T[K] };
type Nullable<T> = { [K in keyof T]: T[K] | null };

Template Literal Types

type EventName<T extends string> = `on${Capitalize<T>}`;
type ClickEvent = EventName<"click">; // "onClick"

type CSSProperty = `${string}-${string}`;
const valid: CSSProperty = "font-size"; // OK

Common Mistakes

  • Over-constraining: Don't add constraints you don't need
  • Generic soup: If you have 4+ type parameters, refactor
  • Using any inside generics defeats the purpose — use unknown

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Why Generics?
  • Constraints
  • Advanced Patterns
  • Conditional Types
  • Mapped Types
  • Template Literal Types
  • Common Mistakes

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.