How to share typescript types between nestjs backend and react frontend?

Viewed 458

Let's say I have a /createPost POST endpoint in my nestjs backend. The request and response object should be fully typed in the backend via the DTO. However, how do I bring these types into the frontend? I would like to have a typed post request object, so I can not enter invalid fields or missing fields in the post body. I would also like to have a typescript interface for the response. I want to REUSE the code from the backend. What's the best way to go about this? Is there some sort of type generator library?

2 Answers

Maybe you can create a monorepo using nx workspace https://nx.dev/. With that you can share models between all apps.

you can use graphql, it can shares types with front

Related