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
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.
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 [
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)4 Replies
@American black bear 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.
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 [
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)Answer
American black bearOP
@joulev, I really appreciate the response.
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
Does importing User as a type solve the problem of accidentally importing the class definition - even in my case?
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 bear <@484037068239142956>, 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?
Yes it should be fine. As long as you only use the entity as a type and not as a class. But to be very safe just use import type as it will error if you accidentally try to use it as a class
American black bearOP
@joulev, perfect. I'll do that then. Thanks for your time!