### Handling Service Implementation Errors in Effect TypeScript Library

Hey guys, still figuring out Effect as I go. I need some help defining services and especially separating the implementation errors from the service interface definition.

The way I see it, different implementations of the same service should be able to have different errors: file-based db could throw FileNotFound while a postgres-based implemention could try FailedToConnect.

export interface AiService {
  readonly processImage: (file: File) => Effect.Effect<string, /* I want this error to be generic/any/inferred by the implementaion. */>
}
export const AiService = Context.GenericTag<AiService>("AiService")

export const AiServiceLive = Layer.effect(
  AiService,
  Effect.gen(function* () { // error: pasted below
    return {
      processImage: (f) =>
        Effect.gen(function* () {
          const file = yield* Schema.decodeUnknown(FileSchema)(f)
          yield* EnforceFileType(file, "image")
          return yield* Effect.succeed(f.name)
        }),
    }
  }),
)


What I've done above gives the following error
Argument of type 'Effect<{ processImage: (f: File) => Effect<string, ParseError | IncorrectFileTypeError | FailedToGetFileTypeError, never>; }, never, never>' is not assignable to parameter of type 'Effect<AiService, never, never>'.
  Type '{ processImage: (f: File) => Effect<string, ParseError | IncorrectFileTypeError | FailedToGetFileTypeError, never>; }' is not assignable to type 'AiService'.
    The types returned by 'processImage(...)' are incompatible between these types.
      Type 'Effect<string, ParseError | IncorrectFileTypeError | FailedToGetFileTypeError, never>' is not assignable to type 'Effect<string, never, never>'.
        Type 'ParseError | IncorrectFileTypeError | FailedToGetFileTypeError' is not assignable to type 'never'.
          Type 'ParseError' is not assignable to type 'never'.ts(2345)


Any comments/help is appreciated 🙂
Was this page helpful?