Typing the return type of a Schema constructor

Hi all! I'm just getting into effect and I figured a good way to do it would be to transform some of my existing zod code. And I've ran into something I didn't quite know how to do. I have a simple interface like:

interface Edge<Node, Cursor> {
  node: Node,
  cursor: Cursor,
}


And I want to write a type constructor for it where one can pass their own schemas for node and cursor. This is pretty simple, but I'm having issues typing the return type to ensure it conforms to the existing Edge interface.

export function EdgeSchema<
  Node extends Schema.Schema.Any, 
  Cursor extends Schema.Schema.Any
>(node: Node, cursor: Cursor) {
  return Schema.Struct({
    cursor,
    node,
  });
}


I've tried adding a satisfies Schema.Schema<Edge<Schema.Schema.Type<Node>, Schema.Schema.Type<Cursor>>> to the Struct call, but that doesn't work. I assume there is something easy I'm missing here haha

I did have to cheat in the zod version (below), but I was hoping I wouldn't have to do that for Schema

export function EdgeParser<NodeParser extends z.ZodTypeAny, CursorParser extends z.ZodTypeAny>(
  node: NodeParser,
  cursor: CursorParser,
): z.ZodSchema<Edge<z.TypeOf<NodeParser>, z.TypeOf<CursorParser>>> {
  // @ts-expect-error z.object doesn't work well with generics
  return z.object({ cursor, node });
}
Was this page helpful?