Conditional Types की जानकारी
In this page:
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
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
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
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
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
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);
T extends string ? ... : ...के runtime पर evaluate होने की उम्मीद करना, जब यह सिर्फ type system में मौजूद है।string | numberजैसा एक union pass करना और result एक union होने से surprised होना, क्योंकि conditional types एक bareTपर distribute होते हैं।inferको एक conditional type केextendsclause के बाहर उपयोग करना, जहां यह allowed नहीं है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates