Effect CommunityEC
Effect Community2y ago
48 replies
addamsson

Extracting generics in `Effect.serviceFunctions`

I have a service that has a few functions and by using Effect.serviceFunctions I can compose them individually:
export class ConfigRepository extends Context.Tag("Service/ConfigRepository")<
    ConfigRepository,
    {
        createMany: (
            configs: UnsavedConfig[],
        ) => Effect.Effect<
            number,
            | EntitiesAlreadyExistsError
            | ValidationError
            | InvalidConfigError
            | DatabaseError,
            TransactionContext
        >;
        // ...
    }
>() {}

export const { createMany, /* ... */ } =
    Effect.serviceFunctions(ConfigRepository);

// usage:
const createMany = () =>
    flow(ConfigRepository.createMany, Transactions.wrap, Effect.either);

this is great, but my problem is that I have some services that have generic functions in them:
export class ConfigLoader extends Context.Tag("Service/ConfigLoader")<
    ConfigLoader,
    {
        loadConfig: <T extends RequiredJsonObject>(
            configProps: ConfigurationProperties<T>,
            filter?: ConfigFilter,
        ) => Effect.Effect<
            T,
            DatabaseError | MissingConfigError | ValidationError,
            Queries.QueryContext
        >;
    }
>() {}

when I use Effect.serviceFunctions with this, the generic type parameter is fixed to RequiredJsonObject. Is there a way to extract the generic too?
Was this page helpful?