Inferring Return Type Based on `include` Parameter

How would I set this up so that Effect properly infers the return type based on the value of include? (So with or without tasks)

export const listProjectsUseCase = ({
  workspaceId,
  query,
  include,
}: {
  workspaceId: typeof WorkspaceId.Type;
  query?: {
    ids?: Array<typeof ProjectId.Type>;
  };
  include?: {
    tasks?: boolean;
  };
}) =>
  Effect.gen(function* () {
    const projectsService = yield* ProjectsService;
    const tasksService = yield* TasksService;

    const projects = yield* projectsService.listProjects({
      workspaceId: workspaceId,
      query: {
        ...(query?.ids && { ids: query.ids }),
      },
    });

    if (include?.tasks) {
      const tasks = yield* tasksService.listTasks({
        workspaceId: workspaceId,
        query: {
          projectIds: projects.map((p) => p.id),
        },
      });

      return projects.map((p) => ({
        ...p,
        tasks: tasks.filter((t) => t.projectId === p.id),
      }));
    }

    return projects;
  });
Was this page helpful?