Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 9 of 56 · Typescript

Generics – Basics

Duration: 25 minutes

Generic Functions

function identity<T>(value: T): T {
  return value;
}

const num = identity(42);   // T inferred as number
const str = identity("hi"); // T inferred as string

Generic Interfaces

interface Pair<T, U> {
  first: T;
  second: U;
}

const p: Pair<number, string> = { first: 1, second: "one" };

Default Generic Types

function wrap<T = string>(value: T) {
  return { value };
}

wrap(123); // T inferred as number
wrap();    // T defaults to string → { value: "" }

Info

Generics give you type‑parameterized abstractions without sacrificing type safety.

Previous: Function OverloadsNext: Generic Constraints