Effect CommunityEC
Effect Community12mo ago
40 replies
HerbCaudill

Multiple possible error types in a generator function?

class TooLongError extends Error {
  _tag = "TooLongError"
  message = "Too long"
}

class TooShortError extends Error {
  _tag = "TooShortError"
  message = "Too short"
}

const length = (input: string) => Effect.succeed(input.length)

const program = (input: string) =>
  Effect.gen(function*() {
    const l = yield* length(input)
    if (l < 3) yield* Effect.fail(new TooShortError())
    if (l > 9) yield* Effect.fail(new TooLongError())
    return "Just right"
  })

(playground)
I would expect the return type of program to be Effect<string, TooShortError | TooLongError> but it is Effect<string, TooShortError>. (If I switch the order of the conditionals, it's Effect<string, TooLongError>.)

Is this to be expected?
Was this page helpful?