Question About Effect Generator "Adapter" and Possible TypeScript Issue

Effect generator "adapter" and odd type issue.

I was previous evaluating using Effect for use in some upcoming projects at my org. I had to shelve it for a couple months but recently dusted it off. Re-reading the docs it seems that the generator "adapter" is no longer referred to (the suggested convention was to use an underscore _, e.g. Effect.gen(function* (_) { ... })). In the docs, I recall it being used to wrap things that returned the Effect.Effect type.

So my first questions are: am I remembering this correctly and is it no longer required?

Secondly, while trying to remove my use of the underscore to see if this all was the case, I ran across something that may be purely a TypeScript issue but I was wondering if anyone else had encountered it and has a solution. What happens is that when assigning a yielded Effect.tryPromise to a const, the compiler/language server emits TS7022. But simply adding the argument back to the generator function clears the error even though it is unused. Here's a minimal reproduction:

class Service extends Context.Tag('Service')<
  Service,
  { mightFail: () => Promise<string> }
>() {}
class TaggedError extends Data.TaggedError('TaggedError') {}

export const fnWithTypeError = () =>
  Effect.gen(function* () {
    const service = yield* Service
    // Type error: 'result' implicitly has type 'any' because it does not have a type annotation and
    // is referenced directly or indirectly in its own initializer.ts(7022)
    const result = yield* Effect.tryPromise({
      try: () => service.mightFail(),
      catch: () => new TaggedError(),
    })
    return result
  })

export const fnWithoutTypeError = () =>
  Effect.gen(function* (_) {
    const service = yield* Service
    // No type error, correct type is inferred
    const result = yield* Effect.tryPromise({
      try: () => service.mightFail(),
      catch: () => new TaggedError(),
    })
    return result
  })


Thanks for your time!
Was this page helpful?