I have an express Typescript app that is the backend for my webapp. The general root structure of the app is
- client
- tsconfig.json
- React code
- server
- application.ts
- tsconfig.json
I need to compile the server files so that I can run ts-node app.ts
. The problem is running tsc
compiles all of the files in the root directory and not just the server
directory. I know I could use exclude
in the tsconfig, but in app.ts
I have type definitions that are imported from front-end
. Is there anyway to only compile the files in the server
directory while being able to keep the imports?
It is good practice to separate your TypeScript type definitions into separate files so that they don't depend on your code. By convention, the *.d.ts file extension is used for this.
I suggest that you make another folder for type definitions and move it so it is a sibling of the server
and client
folders. This way it can be included both in the frontend and backend compilation. However, do think twice before sharing type definitions between your frontend and backend- it can sometimes be the wrong choice if your backend objects need to store more complex logic and your frontend doesn't need to.