You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+15-6
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,7 @@ As you'll see, this library is incredibly minimal _(340B Gzipped)_. The true val
52
52
53
53
-**Conventions over Abstractions**: Minimize abstractions by leveraging existing language features to their fullest.
54
54
-**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.
56
56
-**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.
57
57
-**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.
58
58
@@ -105,7 +105,9 @@ function task() {
105
105
if (condition2) returnnewCustomError2();
106
106
return"value";
107
107
}
108
+
```
108
109
110
+
```typescript
109
111
// In another file...
110
112
const result =task();
111
113
@@ -120,7 +122,9 @@ Since this approach works with plain JavaScript, you can seamlessly integrate ex
120
122
121
123
```typescript
122
124
import { match } from"ts-pattern";
125
+
```
123
126
127
+
```typescript
124
128
match(result)
125
129
.with(P.instanceOf(CustomError1), () => {
126
130
/* Handle CustomError1 */
@@ -133,7 +137,7 @@ match(result)
133
137
});
134
138
```
135
139
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.
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.
154
158
155
159
```typescript
156
160
function task() {
@@ -165,6 +169,7 @@ function task() {
165
169
const result =result1+result2;
166
170
}
167
171
```
172
+
The [$macro](#utility-macro) utility simplifies this process by managing the flow of expected errors across multiple tasks.
168
173
169
174
# Usage
170
175
@@ -188,7 +193,8 @@ function task2(): number | Error2;
188
193
Given these task definitions, we can compute the sum of their results like so:
@@ -238,8 +247,8 @@ Here is a list of known limitations:
238
247
239
248
-`$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.
240
249
- 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.
0 commit comments