Effect CommunityEC
Effect Community3y ago
7 replies
fawwaz

creating utils to run Effects while maintaining strong typing

Hello all 👋 , in my app I have a common pattern across all request where I run the program and them do matching. I wanted to keep my code dry so I created this utility to run the programs
const runProgram = async <
  E extends {
    _tag: string;
  },
  A
>(
  self: Effect.Effect<never, E, A>
) => {
  const res = await Effect.runPromise(Effect.either(self));
  return Either.match(res, {
    onRight: (r) => ({ success: true, value: r } as const),
    onLeft: (e) => {
      return { success: false, errorTag: e._tag } as const;
    },
  });
};

unfortunately , sees that using the runProgram util does not maintain the typing of the _tag as the union of all possible errors inside the program. instead the type of _tag is changed to be of type string. is there is away to handle this without manually passing all possible errors to the util?
image.png
image.png
Was this page helpful?