Fix error typing in AsyncDoEither
This commit is contained in:
parent
fb9a69adc2
commit
a23edf3605
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "fp-lib",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.3",
|
||||
"description": "Zero-dependency functional programming library optimized for Bun",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
|
||||
@ -216,7 +216,6 @@ export class AsyncDoEither<T extends Record<string, unknown>, E> {
|
||||
f: (scope: T) => Either<A, E> | Promise<Either<A, E>>,
|
||||
): AsyncDoEither<T & Record<K, A>, E> {
|
||||
const newPromise = this.promise.then(async (currentEither) => {
|
||||
// Propagate Left case as is
|
||||
if (currentEither._tag === 'Left') {
|
||||
return currentEither as Either<T & Record<K, A>, E>
|
||||
}
|
||||
@ -225,18 +224,15 @@ export class AsyncDoEither<T extends Record<string, unknown>, E> {
|
||||
try {
|
||||
const nextEither = await f(scope)
|
||||
|
||||
// Propagate Left from bound computation
|
||||
if (nextEither._tag === 'Left') {
|
||||
return nextEither as Either<T & Record<K, A>, E>
|
||||
}
|
||||
|
||||
// Extend scope with new value
|
||||
return right({
|
||||
...scope,
|
||||
[key]: nextEither.right,
|
||||
}) as Either<T & Record<K, A>, E>
|
||||
} catch (err) {
|
||||
// Handle unexpected exceptions
|
||||
return left(err as E)
|
||||
}
|
||||
})
|
||||
@ -255,7 +251,7 @@ export class AsyncDoEither<T extends Record<string, unknown>, E> {
|
||||
if (either._tag === 'Left') {
|
||||
return either as Either<B, E>
|
||||
}
|
||||
return right(f(either.right))
|
||||
return right(f(either.right)) as Either<B, E>
|
||||
})
|
||||
}
|
||||
|
||||
@ -308,9 +304,12 @@ export class AsyncDoEither<T extends Record<string, unknown>, E> {
|
||||
|
||||
/**
|
||||
* Starts a new AsyncDoEither chain with an empty scope
|
||||
* @typeParam E - The error type
|
||||
* @returns A new AsyncDoEither instance with an empty scope
|
||||
*/
|
||||
static start<E>(): AsyncDoEither<{}, E> {
|
||||
return new AsyncDoEither(Promise.resolve(right({})))
|
||||
// Fix: Create a Right value with explicit error type casting
|
||||
const initial: Either<{}, E> = right({}) as Either<{}, E>
|
||||
return new AsyncDoEither(Promise.resolve(initial))
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user