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 12 of 56 · Typescript

Conditional Types

Duration: 20 minutes

Conditional Types Syntax

type IsString<T> = T extends string ? true : false;
type Test1 = IsString<number>; // false
type Test2 = IsString<"hello">; // true

Distributive Conditional Types

When the checked type is a union, the conditional distributes over each member.

type ToArray<T> = T extends any ? T[] : never;
type Arr = ToArray<string | number>; // (string[] | number[])

infer Keyword – extracting types

type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type Func = (x: number) => boolean;
type R = ReturnType<Func>; // boolean

Practical Example – DeepPartial

type DeepPartial<T> = {
  [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
};

Info

Conditional types enable type-level programming; use them sparingly to keep code readable.

Previous: Mapped Types & `keyof`Next: Utility Types – `Partial`, `Pick`, `Omit`, `Readonly`