Skip to content

Commit 22a9fac

Browse files
doc: restructure README to explain the return-over-throw paradigm
- Add "The Problem with try/catch" section explaining why catch(e) yields unknown and throw cannot express errors in type signatures - Add "The Idea: Return Errors, Don't Throw Them" section introducing the return-value approach with a findUser example - Replace mathematical examples with a login example (USER_NOT_FOUND, ACCOUNT_LOCKED, WRONG_PASSWORD) to make typed cause more relatable - Use early return pattern instead of if/else in caller examples - Convert API property list to a table with Enumerable column - Move Features section below the problem/solution narrative Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c1972ba commit 22a9fac

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ if (result instanceof Error) {
6161
// result is TaggedError<"USER_NOT_FOUND"> — fully typed
6262
console.error(result.message);
6363
console.error("Searched for id:", result.cause.id); // typed as string
64-
} else {
65-
// result is User here
66-
console.log(result.name);
64+
return;
6765
}
66+
67+
// result is User here
68+
console.log(result.name);
6869
```
6970

7071
No `try/catch`. No type casting. No guessing.
@@ -126,10 +127,11 @@ if (result instanceof Error) {
126127
console.error(`${result.cause.attemptsRemaining} attempts remaining`);
127128
break;
128129
}
129-
} else {
130-
// result is typed as { userId: string; token: string }
131-
console.log("Logged in:", result.userId);
130+
return;
132131
}
132+
133+
// result is typed as { userId: string; token: string }
134+
console.log("Logged in:", result.userId);
133135
```
134136

135137
TypeScript infers the union return type automatically. If you forget to handle

0 commit comments

Comments
 (0)