Discriminated Unions की जानकारी
In this page:
type Shape =
| { kind: "a"; propA: type }
| { kind: "b"; propB: type };
function name(shape: Shape) {
switch (shape.kind) {
case "a": /* ... */ break;
case "b": /* ... */ break;
}
}
Creating a Discriminated Union
एक discriminated union का हर member एक common property share करता है, conventionally type या kind नाम से, पर हर member इसे एक अलग literal value देता है।
TypeScript उस shared discriminant property का उपयोग यह figure out करने के लिए करता है कि यह exactly union के किस member से deal कर रहा है।
उदाहरण: Creating a Discriminated Union
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
type Shape = Circle | Square;
const shape: Shape = { kind: "circle", radius: 5 };
console.log(shape.kind);
Narrowing with the Discriminant
Discriminant property की value check करना, usually एक if statement या switch के साथ, पूरे object को corresponding union member तक narrow करता है।
उस narrowed branch के अंदर, TypeScript उन properties को access होने देता है जो specifically उस एक member से belong करती हैं, पूरे union से नहीं।
उदाहरण: Narrowing with the Discriminant
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
function area(shape: Circle | Square): number {
if (shape.kind === "circle") return 3.14 * shape.radius ** 2;
return shape.side ** 2;
}
console.log(area({ kind: "square", side: 4 }));
Discriminated Unions for Actions
Discriminated unions एक application में distinct actions represent करने के लिए खासकर well-suited हैं, जैसे एक Redux-style reducer, जहां हर action अपना type value रखता है और सिर्फ वे data fields जो उस specific action के लिए relevant हैं।
यह action handling को type-safe और follow करना आसान दोनों रखता है।
उदाहरण: Discriminated Unions for Actions
type Increment = { type: "increment"; amount: number };
type Reset = { type: "reset" };
function reducer(action: Increment | Reset, state: number): number {
if (action.type === "increment") return state + action.amount;
return 0;
}
console.log(reducer({ type: "increment", amount: 5 }, 10));
Multiple Discriminated Members
एक discriminated union में ज़रूरत के हिसाब से कितने भी members हो सकते हैं, और हर एक को discriminant property के लिए अपनी unique literal value उपयोग करनी चाहिए।
जब तक हर value unique है, TypeScript किसी भी संख्या के possible cases के बीच reliably distinguish कर सकता है।
उदाहरण: Multiple Discriminated Members
type A = { kind: "a"; value: number };
type B = { kind: "b"; text: string };
type C = { kind: "c" };
function handle(x: A | B | C) {
console.log(x.kind);
}
handle({ kind: "b", text: "hi" });
Discriminated Unions and Exhaustive Handling
जब एक discriminated union के हर member को explicitly handle कर लिया गया हो, TypeScript एक exhaustiveness check उपयोग करके confirm करने में मदद कर सकता है कि कुछ miss नहीं हुआ, जो अक्सर एक helper function के साथ implement होता है जिसका parameter never के रूप में typed हो।
अगर बाद में union में एक नया member जोड़ा जाए और unhandled छोड़ दिया जाए, यह pattern इसे एक silent runtime bug के बजाय एक compile error के रूप में surface करता है।
उदाहरण: Discriminated Unions and Exhaustive Handling
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
function area(shape: Circle | Square): number {
switch (shape.kind) {
case "circle": return 3.14 * shape.radius ** 2;
case "square": return shape.side ** 2;
default:
const exhaustive: never = shape;
return exhaustive;
}
}
console.log(area({ kind: "circle", radius: 2 }));
- हर member में discriminant के लिए अलग property names उपयोग करना, तो narrowing काम नहीं करता।
- Discriminant को
"circle"जैसे literal के बजाय एक generalstringtype देना। - एक
switchमें एक case भूल जाना, तो union का एक member silently unhandled रह जाता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: