Effect CommunityEC
Effect Community15mo ago
2 replies
brotherdusk

Beginner: How to type a generic decoding function?

if i have a branded type and a decode for it:
export type idVal = Brand.Branded<string, "idVal">;
const decodeVal = Schema.decodeUnknownEither(idVal);

i want to pass decodeVal into decoder
const isInvalidVal = (val: string, decoder: ): boolean => {
  const res = pipe(
    Effect.succeed({ val }),
    Effect.flatMap(decoder), // <--- how to type this?
    runtime.runSyncExit,
  );
  
  if (Exit.isFailure(res)) {
    const failureOption = Cause.failureOption(res.cause);
    if (Option.isSome(failureOption)) {
      const error = failureOption.value;
      return error._tag === "ParseError";
    }
  }
  
  return false;
};


not sure if this should go in typescript channel since its really more of a typescript question, or beginner channel since possibly this entire implementation is incorrect
Was this page helpful?