← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 3 of 20

Conditional Types की जानकारी

Conditional types एक type relationship के आधार पर एक type या दूसरा चुनते हैं। वे T extends U ? X : Y syntax उपयोग करते हैं।
Syntax
typescript
type Name<T> = T extends Condition ? TrueType : FalseType;

Basic Conditional Type

एक conditional type T extends U ? X : Y syntax उपयोग करके जांचता है कि क्या एक type दूसरे को extend करता है, एक type की shape को दो दूसरे types के बीच एक relationship के आधार पर branch होने देते हुए।

उदाहरण: Basic Conditional Type

typescript
type IsString<T> = T extends string ? "yes" : "no";
type A = IsString<string>;
type B = IsString<number>;
const a: A = "yes";
console.log(a);

Conditional Types with Generics

Conditional types खासतौर पर उपयोगी बन जाते हैं जब एक generic type parameter result determine करता है, तो same conditional type definition pass किए गए के आधार पर अलग concrete output types produce कर सकती है।

उदाहरण: Conditional Types with Generics

typescript
type Wrapped<T> = T extends string ? string[] : T[];
function wrap<T>(value: T): Wrapped<T> {
  return [value] as Wrapped<T>;
}
console.log(wrap("hi"));

Conditional Types with Unions

Conditional types अपने आप union members पर distribute होते हैं जब checked type एक bare generic type parameter हो, मतलब T extends U ? X : Y effectively T के रूप में pass किए गए union के हर member के लिए एक बार run होता है।

उदाहरण: Conditional Types with Unions

typescript
type ToArray<T> = T extends any ? T[] : never;
type Result = ToArray<string | number>;
const r: Result = ["a"];
console.log(r);

infer in Conditional Types

infer keyword एक conditional type को check के बीच में दूसरे type का एक हिस्सा capture और name करने देता है — उदाहरण के लिए, एक array type से element type या एक function type से return type extract करना।

उदाहरण: infer in Conditional Types

typescript
type ElementType<T> = T extends (infer U)[] ? U : never;
type Item = ElementType<string[]>;
const item: Item = "hello";
console.log(item);

Practical Conditional Types

Conditional types उपयोगी हैं जब एक reusable type को अलग inputs के लिए अलग तरीके से behave करना चाहिए, जो exactly है कि TypeScript के कई built-in utility types जैसे ReturnType कैसे implement होते हैं।

उदाहरण: Practical Conditional Types

typescript
function greet() {
  return "hi";
}
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type G = MyReturnType<typeof greet>;
const g: G = "hello";
console.log(g);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. T extends string ? ... : ... के runtime पर evaluate होने की उम्मीद करना, जब यह सिर्फ type system में मौजूद है।
  2. string | number जैसा एक union pass करना और result एक union होने से surprised होना, क्योंकि conditional types एक bare T पर distribute होते हैं।
  3. infer को एक conditional type के extends clause के बाहर उपयोग करना, जहां यह allowed नहीं है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.