Runtime Error with `Effect.Service` Accessors in TypeScript Example

I tried to implement Effect.Service accessors in place of a custom solution and it produces runtime errors.

I narrowed it down to this repro using flow.

effect\src\Function.ts:1321
return bc!(ab.apply(this, arguments))
^
TypeError: ab.apply is not a function


import { Effect, flow } from 'effect'

class Service extends Effect.Service<Service>()(
  "MyService",
  { accessors: true, succeed: { a: () => 1 } }
) {}

const Service_a = flow(Service.a, Effect.andThen(a => a))

const program = Service_a().pipe(Effect.provide(Service.Default))

Effect.runPromise(program).catch(console.error)


A quick search showed that I actually don't use accessors this way but I was not able to reproduce the problem differently. I hope it helps anyway

My custom solution:
/**
 * Extract an effectful function from the service.
 * Useful when a feature is reduced to, or is a composition of, this function.
 * JSDoc is lost in the process.
*/
export const extract = <S, I = S>(self: Context.Tag<S, I>) =>
    <K extends Exclude<keyof I, '_tag'>>(key: K) => ((...args: any[]) => self.pipe(
        Effect.andThen(service => (service[key] as (...args: any[]) => unknown)(...args))
    )) as MapReturnType<I[K], $AddRequirement<S>>;
Was this page helpful?