JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstypescript
Next

Learn the concept

Conditional Types

typescript
senior
conditional-types

How do conditional types work in TypeScript?

conditional-types
infer
type-level-programming
advanced
Quick Answer

Conditional types select types based on conditions using the syntax T extends U ? X : Y. They enable type-level programming, allowing different types based on input. Combined with infer, they can extract and transform types dynamically.

Detailed Explanation

Conditional Type Syntax:

  • T extends U ? X : Y
  • If T is assignable to U, type is X, otherwise Y
  • Distributive over unions by default

The 'infer' Keyword:

  • Declares type variable within condition
  • Extracts types from complex structures
  • Used in many utility types (ReturnType, etc.)

Use Cases:

  • Type extraction (unwrapping, getting properties)
  • Type transformation (mapping types)
  • Type filtering (exclude/extract)
  • Building complex utility types

Code Examples

Basic conditional typesTypeScript
// Simple conditional type
type IsString<T> = T extends string ? true : false;

type A = IsString<string>;  // true
type B = IsString<number>;  // false
type C = IsString<'hello'>; // true (literal extends string)

// Conditional with multiple checks
type TypeName<T> = 
  T extends string ? 'string' :
  T extends number ? 'number' :
  T extends boolean ? 'boolean' :
  T extends undefined ? 'undefined' :
  T extends Function ? 'function' :
  'object';

type T1 = TypeName<string>;     // 'string'
type T2 = TypeName<() => void>; // 'function'
type T3 = TypeName<string[]>;   // 'object'

// Distributive behavior with unions
type Stringify<T> = T extends any ? `${T & string}` : never;
type Result = Stringify<'a' | 'b' | 'c'>;
// 'a' | 'b' | 'c' - distributed over each member

Real-World Applications

Use Cases

API Route Type Inference

Inferring request/response types from route handler signatures using conditional types and infer

Deep Type Unwrapping

Recursively unwrapping nested Promises, arrays, or wrapper types for clean type signatures

Type-Level Validation

Building compile-time validators that produce descriptive error types for invalid configurations

Mini Projects

Type-Level JSON Parser

advanced

Implement a type-level JSON parser using recursive conditional types and template literal types

Branded Type System

advanced

Build a branded type system using conditional types for type-safe IDs (UserId, OrderId) that prevent accidental mixing

Industry Examples

tRPC

Uses conditional types extensively for inferring procedure input/output types from router definitions

Type-Challenges

Community-driven TypeScript type challenges that exercise conditional types

Resources

TypeScript - Conditional Types

docs

Related Questions

How do mapped types work in TypeScript?

senior
mapped-types

What are template literal types and how do you use them?

senior
template-literal-types
Next
How do mapped types work in TypeScript?
Next