Function: toThrowable()
Call Signature
toThrowable<
R
,Args
>(fn
): (...args
) =>UnwrapOk
<R
>
Defined in: index.ts:480
Converts a function that returns Results into a function that throws on errors. Useful for integrating Result-based functions with traditional throw-based code.
Type Parameters
R
R
extends Result
<any
, any
>
The Result type returned by the function
Args
Args
extends unknown
[] = []
The argument types of the function
Parameters
fn
(...args
) => NonPromise
<R
>
The function that returns Results
Returns
A new function that throws on Err results and returns data on Ok results
(...
args
):UnwrapOk
<R
>
Parameters
args
...Args
Returns
UnwrapOk
<R
>
Example
function safeDivide(a: number, b: number) {
return b === 0 ? err("Division by zero") : ok(a / b);
}
const divide = toThrowable(safeDivide);
divide(10, 2); // 5
divide(10, 0); // throws "Division by zero"
Call Signature
toThrowable<
R
,Args
>(fn
): (...args
) =>Promise
<UnwrapOk
<R
>>
Defined in: index.ts:483
Converts a function that returns Results into a function that throws on errors. Useful for integrating Result-based functions with traditional throw-based code.
Type Parameters
R
R
extends OrPromise
<Result
<any
, any
>>
The Result type returned by the function
Args
Args
extends unknown
[] = []
The argument types of the function
Parameters
fn
(...args
) => R
The function that returns Results
Returns
A new function that throws on Err results and returns data on Ok results
(...
args
):Promise
<UnwrapOk
<R
>>
Parameters
args
...Args
Returns
Promise
<UnwrapOk
<R
>>
Example
function safeDivide(a: number, b: number) {
return b === 0 ? err("Division by zero") : ok(a / b);
}
const divide = toThrowable(safeDivide);
divide(10, 2); // 5
divide(10, 0); // throws "Division by zero"