Skip to content

Commit 069d533

Browse files
committed
chore: Update README.md
1 parent 7b68e45 commit 069d533

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

README.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ As you'll see, this library is incredibly minimal _(340B Gzipped)_. The true val
5252

5353
- **Conventions over Abstractions**: Minimize abstractions by leveraging existing language features to their fullest.
5454
- **Minimal API**: Strive for simplicity without sacrificing functionality. Conciseness is often an indicator of robust and lasting design.
55-
- **Compatibility and Integrability**: Our solution shouldn't depend on universal adoption, and must seamlessly consume and be consumed by code not written with the same principles in mind.
55+
- **Progressive Enhancement**: Our solution shouldn't depend on universal adoption, and must seamlessly consume and be consumed by code not written with the same principles in mind.
5656
- **Intuitive and Ergonomic**: The patterns should be self-explanatory, allowing developers to grasp and implement them at a glance, minimizing the risk of misinterpretations that could result in anti-patterns or unexpected behaviors.
5757
- **Exploit TypeScript**: Since TypeScript is a de facto standard, we leverage its type system to provide immediate feedback through IDE features like syntax highlighting, error detection, and auto-completion.
5858

@@ -105,7 +105,9 @@ function task() {
105105
if (condition2) return new CustomError2();
106106
return "value";
107107
}
108+
```
108109

110+
```typescript
109111
// In another file...
110112
const result = task();
111113

@@ -120,7 +122,9 @@ Since this approach works with plain JavaScript, you can seamlessly integrate ex
120122

121123
```typescript
122124
import { match } from "ts-pattern";
125+
```
123126

127+
```typescript
124128
match(result)
125129
.with(P.instanceOf(CustomError1), () => {
126130
/* Handle CustomError1 */
@@ -133,7 +137,7 @@ match(result)
133137
});
134138
```
135139

136-
You can progressively enhance your codebase by wrapping third-party methods in tasks. The [$trycatch](#utility-trycatch) utility further enhances this process by eliminating the need for try/catch blocks.
140+
You can progressively enhance your codebase by wrapping third-party methods in tasks.
137141

138142
```typescript
139143
async function $fetch(input: string, init?: RequestInit) {
@@ -150,7 +154,7 @@ async function $fetch(input: string, init?: RequestInit) {
150154
}
151155
```
152156

153-
Composition is also possible, allowing tasks to be chained together while handling expected errors. The [$macro](#utility-macro) utility simplifies this process by managing the flow of expected errors across multiple tasks.
157+
The [$trycatch](#utility-trycatch) utility further enhances this process by eliminating the need for try/catch blocks. Composition is also possible, allowing tasks to be chained together while handling expected errors.
154158

155159
```typescript
156160
function task() {
@@ -165,6 +169,7 @@ function task() {
165169
const result = result1 + result2;
166170
}
167171
```
172+
The [$macro](#utility-macro) utility simplifies this process by managing the flow of expected errors across multiple tasks.
168173

169174
# Usage
170175

@@ -188,7 +193,8 @@ function task2(): number | Error2;
188193
Given these task definitions, we can compute the sum of their results like so:
189194

190195
```typescript
191-
const result: number | Error1 | Error2 = $macro(function* ($try) {
196+
// ?^ result: number | Error1 | Error2
197+
const result = $macro(function* ($try) {
192198
const result1: number = yield* $try(task1());
193199
const result2: number = yield* $try(task2());
194200

@@ -228,6 +234,9 @@ result;
228234
```typescript
229235
// Async functions.
230236
const [result, err] = await $trycatch(async () => { ... });
237+
```
238+
239+
```typescript
231240
// Or Promises.
232241
const [result, err] = await $trycatch(new Promise(...));
233242
```
@@ -238,8 +247,8 @@ Here is a list of known limitations:
238247

239248
- `$trycatch` must be passed functions to be executed, rather than their results. While this isn't as seamless as a language feature would behave, it’s a limitation due to the constraints of JavaScript syntax. However, `$macro` and `$try` do not share this issue.
240249
- Return types must differ from `unknown` or `any`, as these types will obscure the expected error types in the result. You can work around this by wrapping the return value in an object like `{ value }`.
241-
- Errors and Promises have specific roles when used in return types and cannot be treated as generic values. While we believe this is a positive guideline rather than a limitation, you can still resolve this by using `{ value }` as a wrapper.
242-
- Union types of native JavaScript errors will be simplified in TypeScript to a single error type, losing valuable information. For example, `TypeError | RangeError` will be type reduced to `TypeError`. This is a limitation of the TypeScript errors typings and can be addressed by relying on custom errors and wrapping native ones when needed.
250+
- Errors and Promises have specific roles when used in return types and cannot be treated as generic values. While we believe this is a positive guideline rather than a limitation, you can still get around this by using `{ value }` as a wrapper.
251+
- Union types of native JavaScript errors will be simplified in TypeScript to a single error type, losing valuable information. For example, `TypeError | RangeError` will be type reduced to `TypeError`. This is a limitation can be addressed by relying only on custom errors and wrapping native ones when needed.
243252

244253
# License
245254

0 commit comments

Comments
 (0)