Conditional Type Constraints

Conditional Types Constraints also called as Conditional Types Predicates or Type Assertion are a TypeScript feature that allows you to define constraints on generic types within conditional types. They provide a way to narrow down the possible types a generic can be based on a condition. This is particularly useful when you want to perform type checks or assertions within a conditional type.

Syntax

type ConditionalType<T> = T extends OtherConditionalType ? Value 1 : Value 2;

Example: In this example We defines a type CheckNum<T> that checks whether a type T is a number or not. It returns T if it’s a number and never otherwise. Then, it defines another type NumbersOnly<T> which is a mapped type that operates on arrays. It iterates over the keys of the input array type T and uses CheckNum to filter out non-number elements from the array.

Javascript




type CheckNum<T> = T extends number ? T : never;
  
type NumbersOnly<T extends any[]> = {
    [K in keyof T]: CheckNum<T[K]>;
};
  
// Return num
const num: NumbersOnly<[4, 5, 6, 8]> = [4, 5, 6, 8];
  
// Return invalid
const stringnum: NumbersOnly<[4, 6, "7"]> = [4, 6, "7"];


Output:

TypeScript Conditional Types

In this article, we are going to discuss Conditional Types. Conditional types in TypeScript are a powerful feature that allows you to create types that depend on a condition or a set of conditions. As JavaScript is a loosely typed language, conditional types enable you to define types that are based on the values or the properties of other types. It uses the extend keyword and a to define a condition and then produce different types based on whether that condition is true or false. This takes a form that looks a little like conditional expressions (condition ? trueExpression: false expressions.

Syntax

type ConditionalType<T> = T extends Condition ? value 1: value 2;

Where-

  • ConditionalType<T> is the name of the type having <T> type parameter.
  • T extends Condition is the condition that will return true or false
  • Value1 is the value when the condition will be true.

Example: In this example, We define a conditional type Num<T> that checks whether the given type T is an array of numbers, an array of strings, or neither, and returns either number, string, or never accordingly. It then uses this conditional type to declare variables num and ‘stringnum’ with specific type assignments, resulting in type-checking errors because the assignments do not match the expected types, and finally logs the variables.

 

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);


Output:

Similar Reads

Conditional Type Constraints

...

Inferring Within Conditional Types:

Conditional Types Constraints also called as Conditional Types Predicates or Type Assertion are a TypeScript feature that allows you to define constraints on generic types within conditional types. They provide a way to narrow down the possible types a generic can be based on a condition. This is particularly useful when you want to perform type checks or assertions within a conditional type....

Distributive Conditional Types

...

Contact Us