Next.js Discord

Discord Forum

Is it possible to export types out of `server-only` files and import the types in `use client` comp?

Unanswered
Black carp posted this in #help-forum
Open in Discord
Black carpOP
Is it possible to export types out of server-only files and import the types in use client comp?
I tried to just do that beacsue it should work but I get nextjs erorr about server-only import in use client component...

10 Replies

Black carpOP
But in dev I’m getting error is there a specific way of dealing with it?
@alfonsüs ardani
show code?
how did u export it
Black carpOP
@alfonsüs ardani
// client/page.tsx
"use client";
import { type ISchema } from "./validation";
...rest of client code...



// validation.ts
import "server-only";
import z from "zod";

const zodSchema = z.object({ ... });

export type ISchema = z.infer<typeof zodSchema>;

// this is used in a different server component
export default zodSchema;
Pacific sand lance
what error do you get?
also, is there any reason you mark ur schema file as server-only?
there is no reason for using 'server-only' package in validation.ts, if you have to do server action like createUser, then you can create separate file for zod schema and then import it.
In both server as well as client component