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