Inferring Error types from Layer, omitting them in a service types

 
class AIService extends Context.Tag('AIService')<AIService, {
  generateResponse: (
      messages: Messages,
  ) => Effect.Effect<{steps: Array<{text: string}>}, AiError.AiError | DatabaseError>
}>() {}


I have the following service, which may fail with AiError.AiError | DatabaseError. Can I omit specifying error types and create a Layer from which these types will be inferred like with dependencies?

I tried

class AIService extends Context.Tag('AIService')<AIService, {
  generateResponse: (
      messages: Messages,
  ) => Effect.Effect<{steps: Array<{text: string}>}>
}>() {}


const AILayer = Layer.effect(
  AIService,
  Effect.gen(function* () {
    const aiModel = yield* AiLanguageModel;
    const toolkit = yield* SalonToolkit;
    return {
      generateResponse: () => Effect.gen(function* () {
        const response = yield* aiModel.generateText({
          system: prompt,
          prompt: [
            new AiInput.UserMessage({
              parts: [new AiInput.TextPart({text: "I want to book a haircut"})]
            })
          ],
          toolkit
        })

        if(Math.random() > 0.5) {
          yield* Effect.fail(new DatabaseError({operation: '', message: '',cause: ''}))
        }
        
        yield* Effect.logInfo(response)
        return { steps: []}
      })
    }
  })
)


However this gives TS errors because Layer throws some errors, which are not indicated in a service type
Was this page helpful?