TypeScript के Advanced Types
In this page:
type Name<T> = /* type expression using T */;
Intersection Types Overview
एक intersection type, & से लिखा जाता है, कई types को एक में combine करता है जिसे एक साथ हर combined type satisfy करना चाहिए, तो A & B के रूप में typed एक value को दोनों A और B की सारी properties चाहिए बजाय सिर्फ किसी एक से match करने पर valid होने के।
उदाहरण: Intersection Types Overview
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;
const user: Person = { name: "Ravi", age: 25 };
console.log(user);
Discriminated Unions
एक discriminated union object types का एक union है जो एक common literal property share करते हैं, जिसे अक्सर kind या status कहा जाता है, जिसे TypeScript एक if या switch check के अंदर union को एक specific member तक narrow करने के लिए उपयोग करता है, उस member की unique properties तक safe access देते हुए।
उदाहरण: Discriminated Unions
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; side: number };
function area(shape: Shape): number {
if (shape.kind === "circle") return Math.PI * shape.radius ** 2;
return shape.side ** 2;
}
console.log(area({ kind: "square", side: 4 }));
Template Literal Types
Template literal types backticks के अंदर दूसरे literal या union types interpolate करके नए string literal types बनाते हैं, बिल्कुल JavaScript template strings जैसे, तो Direction और Side जैसे दो unions combine करना हर possible dash-joined combination को एक type के रूप में produce करता है।
उदाहरण: Template Literal Types
type Direction = "top" | "bottom";
type Side = "left" | "right";
type Corner = `${Direction}-${Side}`;
const c: Corner = "top-left";
console.log(c);
Recursive Types
एक recursive type अपनी ही definition में खुद को refer करता है, जो TypeScript को JSON values या nested arrays जैसी deeply nested या self-similar structures describe करने देता है, जहां किसी भी level पर एक value खुद उसी shape की और values रख सकता है।
उदाहरण: Recursive Types
type Json = string | number | boolean | Json[] | { [key: string]: Json };
const data: Json = { name: "Ravi", tags: ["a", "b"], nested: { active: true } };
console.log(data);
Combining Advanced Type Features
Advanced type features शायद ही अलग-अलग उपयोग होते हैं: real-world type definitions अक्सर APIs, state machines, और दूसरी structures को precisely model करने के लिए intersections, discriminated unions, template literals, और mapped types को एक साथ combine करती हैं।
उदाहरण: Combining Advanced Type Features
type Event =
| { type: "click"; target: `#${string}` }
| { type: "keydown"; key: string };
function handle(e: Event) {
if (e.type === "click") console.log("Clicked", e.target);
else console.log("Key", e.key);
}
handle({ type: "click", target: "#button" });
string & numberजैसे incompatible primitives intersect करना, जोneverतक reduce हो जाता है तो कोई value इसे satisfy नहीं कर सकता।- Literal
kindproperty भूल जाना (या इसे plainstringटाइप करना), तो union discriminated नहीं है और narrowing काम नहीं करती। - एक ऐसा recursive type लिखना जिसमें एक cycle हो जो कभी खत्म न हो, जैसे
type A = A | stringstyle aliases, बजाय recursion को एक object या array के अंदर रखने के।
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