Constraining Success Types in TypeScript Effect Handlers While Keeping Error and Context Unrestra...

Hi, I'm new to effect and wonder: How can I restrain the success type? I want to type a handler function that produces an effect but constrain the success type. However when I constrain the success type, how can I keep error and context of the effect unrestrained?

Example:

import { Context, Effect } from "effect";

export class ExternalDep extends Context.Tag("ExternalDep")<
  ExternalDep,
  { bar: Effect.Effect<{ foo: string }> }
>() {}

type Event<Payload = void> = { id: string; payload: Payload };
export type Handler = (input: { foo: string }) => Effect.Effect<Event<any>[]>;

const handler1: Handler = ({ foo }) =>
  Effect.gen(function* () {
    return [{ id: "test", payload: undefined }];
  });

// Not working because of the error
const handler2: Handler = ({ foo }) =>
  Effect.gen(function* () {
    yield* Effect.fail(new Error("Test"));
    return [{ id: "test", payload: { foo: "bar" } }];
  });

// Not working because of the dependency
const handler3: Handler = ({ foo }) =>
  Effect.gen(function* () {
    const dep = yield* ExternalDep;
    const foo = yield* dep.bar;
    return [{ id: "test", payload: { foo } }];
  });
Was this page helpful?