Effect CommunityEC
Effect Community3y ago
12 replies
tomchristensen

Safety of Throwing Yieldable Errors

The new yieldable errors are a great addition! Is it safe to throw yield one of these things?

Basically I have a couple of places where I want to yield an error if some condition fails, like this:

import { Effect, Error } from 'effect'

class NoFooError extends Error.Tagged('NoFooError')<{}> {}

const fooCheck = (foo: string | undefined) =>
  Effect.gen(function* (_) {
    if (!foo) {
      yield* _(new NoFooError())
    }
    return foo
  })


However since TS isn't aware that Effect will bail early on error,
foo
is still string | undefined after the if-condition. If I instead do:
const fooCheckWithThrow = (foo: string | undefined) =>
  Effect.gen(function* (_) {
    if (!foo) {
      throw yield* _(new NoFooError())
    }
    return foo
  })

Then the type is narrowed appropriately.

Running the above two snippets with an
undefined
input gives the same failure, but I'm wondering if there are any subtle differences in runtime behaviour for more complex programs?
Was this page helpful?