← Back to TypeScript Course | Chapter 18: Error Handling | Lesson 4 of 5

Result Type Pattern की जानकारी

Result type pattern success और failure को explicit values के रूप में represent करता है बजाय सिर्फ thrown exceptions पर निर्भर रहने के। एक discriminated union दोनों outcomes को सुरक्षित रूप से handle करना संभव बनाता है।
Syntax
typescript
type Result<T, E> =
  | { ok: true; value: T }
  | { ok: false; error: E };

Defining a Result Type

एक Result type में आमतौर पर एक ok result होता है जो एक value carry करता है या एक error result जो एक error value carry करता है — success और failure सिर्फ एक thrown exception से discoverable होने के बजाय explicit data बन जाते हैं।

उदाहरण: Defining a Result Type

typescript
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
const success: Result<number, string> = { ok: true, value: 42 };
console.log(success);

Handling Success and Failure

value या error access करने से पहले ok property check करें — यह exactly वह shape है जो TypeScript को एक discriminated union safely narrow करने के लिए चाहिए, इसलिए compiler आपको एक failed Result पर value पढ़ने से actually रोक देगा।

उदाहरण: Handling Success and Failure

typescript
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
function divide(a: number, b: number): Result<number, string> {
  return b === 0 ? { ok: false, error: "Division by zero" } : { ok: true, value: a / b };
}
const result = divide(10, 2);
if (result.ok) console.log(result.value);
else console.log(result.error);

Result with Custom Errors

एक Result का error side एक custom error class या किसी भी other structured error type को hold कर सकता है, आपको throw करने का सहारा लिए बिना एक failure के बारे में जितना detail चाहिए उतना carry करने देते हुए।

उदाहरण: Result with Custom Errors

typescript
class ValidationError extends Error {}
type Result<T> = { ok: true; value: T } | { ok: false; error: ValidationError };
function parseAge(input: string): Result<number> {
  const n = Number(input);
  return isNaN(n) ? { ok: false, error: new ValidationError("bad age") } : { ok: true, value: n };
}
console.log(parseAge("25"));

Result Helper Functions

छोटे helper functions — ok(value) और err(error) — Result values को जल्दी बनाना आसान बनाते हैं और call sites को readable रखते हैं बजाय हर बार manually object literal बनाने के।

उदाहरण: Result Helper Functions

typescript
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
function ok<T>(value: T): Result<T, never> { return { ok: true, value }; }
function err<E>(error: E): Result<never, E> { return { ok: false, error }; }
console.log(ok(5), err("failed"));

When to Use Result

Result तब shine करता है जब failures expected, routine outcomes हों — एक validation failure, एक not-found lookup — और callers को एक thrown exception को ignore कर पाने के बजाय उन्हें explicitly handle करने के लिए force किया जाना चाहिए।

उदाहरण: When to Use Result

typescript
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
function findUser(id: number): Result<string, string> {
  return id === 1 ? { ok: true, value: "Ravi" } : { ok: false, error: "not found" };
}
const result = findUser(2);
console.log(result.ok ? result.value : result.error);
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. पहले result.ok check किए बिना result.value access करना, जो TypeScript reject करता है।
  2. एक function के अंदर exceptions throw करना जो एक Result return करने का promise करता है, इसलिए callers type पर trust नहीं कर सकते।
  3. ok: boolean declare करना बजाय literals true / false के, इसलिए union discriminated नहीं है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

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.