user1790300
user1790300

Reputation: 1745

type error for typescript fp-ts code when using the pipe function

I am attempting to write code using fp-ts functional programming library and keep receiving the following type error:

TS2322: Type unknown is not assignable to type TaskEither<ErrorBase, unknown> TaskEither. d. ts(1340, 42): The expected type comes from the return type of this signature.

  1. What is the issue as I not sure what the error is telling me the problem is?

  2. How do I fix this error?

Any help would be greatly appreciated.

The highlighted part of the code below seems to be the source of the error:

const findByUserAndGroup = groupMemberRepository.findByUserAndGroup(cancelGroupMembershipDTO.groupId);

pipe(
    sequenceS(ApplyPar)({
        user: findByUserAndGroup(cancelGroupMembershipDTO.userId),
        userToCancel: findByUserAndGroup(cancelGroupMembershipDTO.userToCancelId)
    }),
    chain(({ user, userToCancel }) =>
        **pipe(
            user,
            isGroupOwner,
            fromEither,
            map(() => userToCancel)
        )**
    ),
    map((userToCancel) => ({
        groupMemberId: userToCancel.groupMemberId,
        groupId: userToCancel.groupId,
        userId: userToCancel.userId,
        isGroupOwner: userToCancel.isGroupOwner
    })),
    chain((processedDetails) => userRepo.delete(processedDetails))
)

findByUserAndGroup:

const findByUserAndGroup = (groupMemberId: string) => (userId: string): TaskEither<ErrorBase, Option<GroupMember>> => {
    return pipe(
        tryCatch(
            () => pool.query(
                `select *
                from groupMember
                where groupMemberId = ${groupMemberId} and userId = ${userId}
                `
            ),
            (error: any) => ({message: `Database error: ${String(error)}`, name: 'SystemError', details: 'Database Error.', innerError: error})
        ),
        chain(result => right(none))
    );
}

IsGroupOwner validation function:

export const isGroupOwner = (groupMember: GroupMember): TaskEither<ErrorBase, GroupMember> => {
    if (!groupMember.isGroupOwner) {
        return left({message: `You do not have the permissions necessary to perform this operation.`)});
    }

    return right(groupMember);
}

Upvotes: 5

Views: 68

Answers (0)

Related Questions