Home > OS >  How do we keep our typescript types sync between frontend and backend (React and node)
How do we keep our typescript types sync between frontend and backend (React and node)

Time:01-06

I a using React as my frontend and Nodejs as my backend. Many cases I find myself writing the same typescript definition for my server side response and frontend. Is there a way to share the typescript definition between the server and client

interface DadJokes {
  id:string;
  joke: string;
}

When you have big project and you change something on the server side types, sometimes it creates issues when you forget to change it on the client or you make a typo

CodePudding user response:

  1. Using TypeScript's tsc compiler you can generate declarations files for your shared types, which can then be included in both your frontend and backend projects. This can be a good option if you want to keep your shared types in a separate package or library.
  2. Using git submodules you can include a git repository as a subdirectory of another repository.
  3. Using bit.dev you can share code components, including TypeScript types.

CodePudding user response:

one solution is to create your own npm packaga and install it in both projects.

learn more here .

  • Related