Next.js Discord

Discord Forum

If TS classes from server files are used as types in client files, are they sent to the browser?

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I have a Next.js application that uses the app router. I am also using a custom express server where I use TypeORM to handle DB entities. I thought it was a good idea to directly use an entity class (a class whose properties directly map to columns in the DB) as a type in a client (.tsx) file, so that I won't have to duplicate types.

I have two questions:

1. First of all, is this bad practice? Should I write type definitions separately for the client?

2. More importantly, is it possible for my TypeORM classes, or any of my server code, to somehow end up in the browser?

Thank you so much for your time.
Answered by joulev
1. no don't worry

2. if you don't do weird things like having a server-side entity (function/variable/class) with the same name as a type, then no it won't end up in the browser. type imports are stripped at build time.

to be doubly sure, or if you use same name for your type and your class, then you can use [import type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
View full answer

4 Replies

Answer
American black bearOP
@joulev, I really appreciate the response.

import type definitely feels like a good alternative, and I'll certainly try that.

As for your concern about me doing weird things like naming a class the same as a type - in my case, the class is the type. For instance, there is a class User in the server directory, which I am directly using in my page.tsx file like so: user: User.

Does importing User as a type solve the problem of accidentally importing the class definition - even in my case?
American black bearOP
@joulev, perfect. I'll do that then. Thanks for your time!