How to create conditional types in TypeScript ?
Last Updated :
26 Apr, 2024
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 types with the used of the ternary operator and extend in TypeScript.
Type1 extends Type2 ? for One value : for different value;
Here extend works as a compare function that checks is Type1 has properties of Type2 if yes then it jumps to the true branch else to the false branch.
Now let's understand through the following examples.
Example 1: Basic Conditional Types
In this example, we will create the Conditional types which take the values and check it has the property of number or not with extends. If it has the property of number conditional types assign the types to the caller of conditional type else it assigns never to types.
JavaScript
// Distributive property of Typescript
type return_dis<D>= D extends number ? D : never;
type show = number;
// Conditional types for number
type new_number = return_dis<show>;
let n1 : new_number = 88;
console.log(n1);
console.log(typeof n1)
Output:
88
number
Example 2: Narrowing Functionality of Input Data
Now, we will create the conditional type by narrowing the functionality of input data. In this type of creating conditional types, we will be filtering out the specific input type which contains some value. Here, we use extended function and set of values for narrowing with conditional types. First we will create the conditional types which checks the property if it is has a number string or Boolean or not then if it has
JavaScript
type Conditional<G> = G extends {typeof : number|string|Boolean} ?
G :"This is an error";
let n = 55;
type nu = Conditional<typeof n>;
let s = "hello world";
type Str = Conditional<s>;
let b = Boolean;
type Boo = Conditional<typeof b>;
let k = null;
type SecondCond = Conditional<typeof k>;
let l1: nu = 88;
console.log(l1);
let l2: Str = "Hello Geeks";
console.log(l2);
let l3: Boo = true;
console.log(l3);
let l: SecondCond = "This is an error";
console.log(l);
Output:
88
Hello Geeks
true
This is an error
Example 3: Using Conditional Types for Multiple Data Types
In this example first, we will create the conditional types for the number and string in one conditional type. After that, we use the same conditional types for both string and number.
JavaScript
// Distributive property of Typescript
type return_dis<D>= D extends number|string ? D : never;
type show = number|"Geek";
// Conditional types for number
type new_number = return_dis<show>;
let n1 : new_number = 88;
console.log(n1);
// Same type is used for string also
type new_Geek = return_dis<show>;
let G1 : new_Geek = "Hey Geeks";
console.log(G1)
Output:
88
Hey Geeks
Similar Reads
TypeScript Conditional Types 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.Conditiona
4 min read
How to Create an Interface with Condtional Type ? Conditional types in TypeScript offer developers the ability to craft interfaces that adapt their behavior based on the types they encounter. This feature enhances code flexibility and adaptability, particularly in situations where types may vary. By employing conditional types, developers can defin
3 min read
How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar
3 min read
How To Get Types From Arrays in TypeScript? In TypeScript, arrays are a common data structure, but sometimes it's necessary to extract the types of elements stored in an array for type-checking or validation purposes. TypeScript provides several ways to extract types from arrays, enabling more type-safe operations.We will explore different me
3 min read
How to express a Date Type in TypeScript ? In TypeScript, the Date object is used to handle dates and times. To express a Date type, declare a variable with the type annotation Date and assign it a date value. This allows for precise date manipulation and formatting in your TypeScript code.Ways to Express Date Type in TypeScriptTable of Cont
3 min read