Skip to content

Commit 2ef8f6d

Browse files
committed
docs: add JSDoc for TryCatch types
This improves: - Provides clear descriptions for `Success`, `Failure`, `Result`, and related types. - Documents generic parameters and return types for better type clarity. - Enhances developer experience with detailed function annotations.
1 parent 5c990b6 commit 2ef8f6d

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

packages/try-catch-tuple/src/tryCatch.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,94 @@ type DataErrorTuple<T, E> = Branded<
77
DisableArrayMethods<[data: T, error: E] & never[]>
88
>;
99

10+
/**
11+
* Represents a successful result where `data` is present and `error` is `null`.
12+
*/
1013
export type Success<T> = DataErrorTuple<T, null>;
14+
15+
/**
16+
* Represents a failure result where `error` contains an error instance and `data` is `null`.
17+
*/
1118
export type Failure<E extends Error> = DataErrorTuple<null, E | Error>;
19+
20+
/**
21+
* Represents the result of an operation that can either succeed with `T` or fail with `E`.
22+
*/
1223
export type Result<T, E extends Error> = Success<T> | Failure<E>;
1324

25+
/**
26+
* Resolves the return type based on whether `T` is a promise:
27+
* - If `T` is a `Promise<U>`, returns `Promise<Result<U, E>>`.
28+
* - Otherwise, returns `Result<T, E>`.
29+
*/
1430
export type TryCatchResult<T, E extends Error> = T extends Promise<infer U>
1531
? Promise<Result<U, E>>
1632
: Result<T, E>;
1733

34+
/**
35+
* Function type for handling try-catch logic.
36+
*
37+
* @template E_ Default error type.
38+
* @template T The return type of the function being executed.
39+
* @template E The error type that extends the default error type.
40+
*
41+
* @param fn A function, promise, or value to execute within a try-catch block.
42+
* @param operationName Optional name added to `error.message` for better debugging and context.
43+
*/
1844
export type TryCatchFunc<E_ extends Error = Error> = <T, E extends Error = E_>(
1945
fn: T | (() => T),
2046
operationName?: string,
2147
) => TryCatchResult<T, E>;
2248

49+
/**
50+
* A utility for handling synchronous and asynchronous operations within a try-catch block.
51+
*
52+
* @template F The function type for try-catch execution.
53+
* @template E_ The base error type.
54+
*/
2355
export type TryCatch<
2456
F extends TryCatchFunc = TryCatchFunc,
2557
E_ extends Error = Error,
2658
> = F & {
59+
/**
60+
* Executes a synchronous function inside a try-catch block.
61+
*
62+
* @param fn The function to execute.
63+
* @param operationName Optional name added to `error.message` for better debugging and context.
64+
* @returns A `Result<T, E>` indicating success or failure.
65+
*/
2766
sync: <T, E extends Error = E_>(
2867
fn: () => T,
2968
operationName?: string,
3069
) => Result<T, E>;
70+
71+
/**
72+
* Executes an asynchronous function inside a try-catch block.
73+
*
74+
* @param fn The function or promise to execute.
75+
* @param operationName Optional name added to `error.message` for better debugging and context.
76+
* @returns A `Promise<Result<T, E>>` indicating success or failure.
77+
*/
3178
async: <T, E extends Error = E_>(
3279
fn: Promise<T> | (() => Promise<T>),
3380
operationName?: string,
3481
) => Promise<Result<T, E>>;
82+
83+
/**
84+
* Creates a new `TryCatch` instance that handles additional error types.
85+
*
86+
* @template E Extends the existing error type.
87+
* @returns A new `TryCatch` instance with extended error handling capabilities.
88+
*/
3589
errors: <E extends Error>() => TryCatch<TryCatchFunc<E | E_>, E | E_>;
3690
};
3791

3892
/**
3993
* tryCatch - Error handling that can be synchronous or asynchronous
4094
* based on the input function.
4195
*
42-
* @param fn Function to execute, Promise or value.
43-
* @param operationName Optional name for context.
96+
* @param fn A function, promise, or value to execute within a try-catch block.
97+
* @param operationName Optional name added to `error.message` for better debugging and context.
4498
* @returns A Result, or a Promise resolving to a Result, depending on fn.
4599
*/
46100
export const tryCatch: TryCatch = <T, E extends Error = Error>(

0 commit comments

Comments
 (0)