Skip to content

Commit

Permalink
Type fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoci committed Sep 28, 2023
1 parent 3cbeb54 commit 6776ba2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/either/either.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('Either', () => {

describe('mapLeft', () => {
it('supports right return', () => {
const newAdt = right(999)
const newAdt = right<Error, number>(999)

const actual = adt.mapLeft(() => newAdt)

Expand Down
10 changes: 8 additions & 2 deletions src/either/either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const createRight = <E, A>(data: A) =>
),
fold: <B>(_: (left: E) => B, ifRight: (right: A) => B) => ifRight(data),
getOrElse: () => data,
// map: (ifRight) => rightOf<E, any>(ifRight(data)),
flatMap: (ifRight) => rightOf<E, any>(ifRight(data)),
mapLeft: () => rightOf(data),
})
Expand All @@ -42,6 +43,7 @@ const createLeft = <E, A>(data: E) => {
),
fold,
getOrElse: fold,
// map: () => leftOf(data),
flatMap: () => leftOf(data),
mapLeft: (ifLeft) => leftOf<any, A>(ifLeft(data)),
})
Expand All @@ -51,6 +53,10 @@ export const rightOf = <E, A>(data: A): Either<E, A> => (isEither<E, A>(data) ?

export const leftOf = <E, A>(data: E): Either<E, A> => (isEither<E, A>(data) ? data : createLeft(data))

export const right = createRight
export const right = <E = [Error, 'Specify Left type in right()'], A = [Error, 'Specify Right type in right()']>(
data: A
) => createRight<E, A>(data)

export const left = createLeft
export const left = <E = [Error, 'Specify Left type in left()'], A = [Error, 'Specify Right type in left()']>(
data: E
) => createLeft<E, A>(data)
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ interface EQ<E, A> {
equals(value: Either<E, A>): boolean
}

// type AnyNonEither = Exclude<any, Either<any, any>>

interface Chainable<E, A> {
flatMap<B>(ifRight: (right: A) => Either<E, B>): Either<E, B>
// map<B extends AnyNonEither = A>(ifRight: (right: A) => B): Either<E, B>

flatMap<B = A>(ifRight: (right: A) => Left<E, A> | Right<E, B>): Either<E, B>

mapLeft<G>(ifLeft: (left: E) => Either<G, A>): Either<G, A>
mapLeft<G = E>(ifLeft: (left: E) => Left<G, A> | Right<E, A>): Either<G, A>
}

interface Foldable<E, A> {
Expand Down

0 comments on commit 6776ba2

Please sign in to comment.