@shkumbinhsn/try-catch 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -93,6 +93,35 @@ if (error) {
93
93
  }
94
94
  ```
95
95
 
96
+ ### Using `tc()` for Inferred Return Types
97
+
98
+ When you want TypeScript to infer your return type but still declare possible errors, use the `tc()` helper:
99
+
100
+ ```typescript
101
+ import { tryCatch, tc } from "@shkumbinhsn/try-catch";
102
+
103
+ class APIError extends Error {}
104
+ class NetworkError extends Error {}
105
+
106
+ function fetchUser() {
107
+ const user = { id: "1", name: "Ada", email: "ada@example.com" };
108
+ // Return type is inferred: { id: string; name: string; email: string } & Throws<APIError | NetworkError>
109
+ return tc(user, [APIError, NetworkError]);
110
+ }
111
+
112
+ const [data, error] = tryCatch(fetchUser);
113
+
114
+ if (error) {
115
+ console.log("Failed:", error.message);
116
+ // TypeScript knows: error is Error | APIError | NetworkError
117
+ } else {
118
+ console.log("User:", data.email);
119
+ // TypeScript knows: data is { id: string; name: string; email: string }
120
+ }
121
+ ```
122
+
123
+ This is useful when you don't want to manually specify complex return types but still want typed errors.
124
+
96
125
  ### Without Explicit Error Types
97
126
 
98
127
  When you don't specify error types, the library falls back to standard TypeScript inference:
@@ -137,11 +166,31 @@ function myFunction(): ReturnType & Throws<MyError> {
137
166
  }
138
167
  ```
139
168
 
169
+ ### `tc<T, E>(value: T, errors: E[]): T & Throws<...>`
170
+
171
+ Brands a return value with error types while letting TypeScript infer the return type.
172
+
173
+ **Parameters:**
174
+ - `value`: The value to return
175
+ - `errors`: Array of error class constructors (used only for type inference)
176
+
177
+ **Returns:**
178
+ - The same value, branded with error types
179
+
180
+ **Usage:**
181
+ ```typescript
182
+ function myFunction() {
183
+ const result = computeSomething();
184
+ return tc(result, [ErrorA, ErrorB]);
185
+ }
186
+ ```
187
+
140
188
  ## Limitations
141
189
 
142
190
  - **Duplicate Definitions**: Error types must be declared in both the throw statement and return type
143
191
  - **Runtime Validation**: No runtime enforcement of declared error types
144
192
  - **Learning Curve**: Requires understanding of TypeScript's structural typing
193
+ - **Type Stripping**: TypeScript cannot reliably extract base types from branded intersections ([TS#62985](https://github.com/microsoft/TypeScript/issues/62985)). Error classes with custom instance properties may cause inconsistent type inference.
145
194
 
146
195
  ## Contributing
147
196
 
@@ -17,3 +17,12 @@ type TryCatchReturn<T> = T extends Promise<infer R>
17
17
  : DataError<T>;
18
18
 
19
19
  export declare function tryCatch<T>(fn: () => T): TryCatchReturn<T>;
20
+
21
+ type ErrorConstructor = new (...args: any[]) => Error;
22
+
23
+ type UnionFromTuple<T extends ErrorConstructor[]> = InstanceType<T[number]>;
24
+
25
+ export declare function tc<T, E extends ErrorConstructor[]>(
26
+ value: T,
27
+ errors: [...E]
28
+ ): T & Throws<UnionFromTuple<E>>;
package/lib/try-catch.js CHANGED
@@ -26,4 +26,21 @@ export function tryCatch(fn) {
26
26
  } catch (e) {
27
27
  return [null, e]
28
28
  }
29
+ }
30
+
31
+ /**
32
+ * Brands a return value with potential error types for tryCatch inference.
33
+ *
34
+ * @param {T} value - The value to return
35
+ * @param {Array} _errors - Array of error constructors (used only for type inference)
36
+ * @returns {T} The same value, branded with error types
37
+ *
38
+ * @example
39
+ * function fetchUser() {
40
+ * const user = getUser();
41
+ * return tc(user, [APIError, NetworkError]);
42
+ * }
43
+ */
44
+ export function tc(value, _errors) {
45
+ return value;
29
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shkumbinhsn/try-catch",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "A utility package for handling try-catch blocks in TypeScript.",
5
5
  "main": "./lib/try-catch.js",
6
6
  "module": "./lib/try-catch.js",