Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
TypeScript Conditional Types
Next article icon

TypeScript Conditional Types

Last Updated : 24 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions.

  • They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.
  • Conditional types are particularly useful for creating utility types and for advanced type manipulations, enhancing code reusability and type safety.
JavaScript
type IsString<T> = T extends string ? 'Yes' : 'No';  type Test1 = IsString<string>; type Test2 = IsString<number>;  console.log('Test1:', 'Yes'); console.log('Test2:', 'No'); 
  • The IsString type alias uses a conditional type to check if a type T extends the string.
  • If T is assignable to string, it resolves to 'Yes'; otherwise, it resolves to 'No'.
  • Test1 is evaluated as 'Yes' because the string extends the string.
  • Test2 is evaluated as 'No' because the number does not extend the string.

Output:

Test1: Yes
Test2: No
JavaScript
type Num<T> = T extends number[] ? number      : (T extends string[] ? string : never)      // Return num const num: Num<number[]> = 4;  // Return invalid const stringnum: Num<number> = "7";  console.log(num, stringnum); 

Conditional Type Constraints

Conditional type constraints allow defining constraints on generic types within conditional types, enabling dynamic and precise type handling.

JavaScript
type CheckNum<T> = T extends number ? T : never;  type NumbersOnly<T extends any[]> = {   [K in keyof T]: CheckNum<T[K]>; };  const num: NumbersOnly<[4, 5, 6, 8]> = [4, 5, 6, 8]; const invalid: NumbersOnly<[4, 6, "7"]> = [4, 6, "7"]; 
  • CheckNum<T> ensures only numbers are retained; other types resolve to never.
  • NumbersOnly<T> applies CheckNum to each array element, filtering non-numbers.

Output:

Type '"7"' is not assignable to type 'never'.

Inferring Within Conditional Types

This feature extracts and utilizes types within a conditional type definition, enabling precise transformations.

JavaScript
type ElementType<T> = T extends (infer U)[] ? U : never;  const numbers: number[] = [1, 2, 3]; const element: ElementType<typeof numbers> = numbers[0]; const invalidElement: ElementType<typeof numbers> = "string"; 
  • ElementType<T> extracts element types from arrays using the infer keyword.
  • element correctly resolves to number; assigning a string is invalid.

Output:

Type 'string' is not assignable to type 'number'.

Distributive Conditional Types

These types distribute over unions, applying conditional logic to each union member individually.

JavaScript
type Colors = 'red' | 'blue' | 'green';  type ColorClassMap = {   red: 'danger';   blue: 'primary';   green: 'success'; };  type MapColorsToClasses<T extends string> = T extends keyof ColorClassMap   ? { [K in T]: ColorClassMap[T] }   : never;  const redClass: MapColorsToClasses<Colors> = { red: 'danger' }; const invalidClass: MapColorsToClasses<Colors> = { yellow: 'warning' }; 
  • MapColorsToClasses<T> checks if T matches a key in ColorClassMap, mapping it accordingly.
  • Invalid colors like 'yellow' are rejected because they do not exist in ColorClassMap.

Output:

Type '{ yellow: "warning"; }' is not assignable to type 'never'.

Best Practices of Using TypeScript Conditional Types

  • Use conditional types to create flexible, reusable type definitions.
  • Combine conditional types with generics for enhanced adaptability.
  • Utilize the infer keyword for type inference in complex scenarios.

Next Article
TypeScript Conditional Types

A

ameyabavkar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • TypeScript
  • Geeks Premier League 2023

Similar Reads

    Data types in TypeScript
    In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim
    3 min read
    How to create conditional types in TypeScript ?
    Conditional types in TypeScript enable defining types based on conditions, similar to conditional statements in code. They determine different types of values, making functions adaptable to various input types, and enhancing code flexibility and maintainability. Syntax: We can create conditional typ
    3 min read
    TypeScript Constraints
    TypeScript constraints are used in generic type declarations to restrict the types that can be used as type arguments for the generic. Constraints allow you to specify that a generic type parameter must meet certain criteria, such as implementing a particular interface, having a specific method, or
    2 min read
    Type Manipulation in TypeScript
    TypeScript offers strong tools for types manipulation and transformation, these tools allow programmers to create new types by composing, intersecting, unionizing, mapping and conditioning existing ones, in this article we will investigate some of the advanced type-manipulation features in TypeScrip
    3 min read
    TypeScript Object Types
    TypeScript object types define the structure of objects by specifying property types, ensuring type safety and clarity when passing objects as function parameters.Optional properties, denoted with a ? provide flexibility for objects with varying properties. This approach enhances code robustness by
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences