Are you seeking a hassle-free method to share Typescript types between projects without additional complexities? Look no further! In this blog post, I’ll walk you through a streamlined approach that will save you time and effort while supercharging your REST API development. You don’t need any additional tooling, frameworks, or libraries.
Many blog posts cover this topic, but most involve additional tooling, which can be overwhelming. I’m excited to share a much simpler and more efficient technique that will make your life easier. Say goodbye to unnecessary complexities and embrace a more straightforward way to handle Typescript types. I already created a twitter thread on this topic, but thought it would good idea to turn it into a blog post. Let’s dive in together! 🏊♀️
Perhaps, you’re already familiar with schema validation libraries that you can use to validate request parameters and responses. Most of these libraries are using JSON.parse static function. The problem is that JSON.parse is a blocking I/O operation that blocks Node.js main loop. If you have big payloads or need to validate large objects, you can have performance issues.
Profiling node.js with Zod
If you use GraphQL or Swagger, sharing API types between client and server is pretty easy. For example, GraphQL has a tool graphql-codegen that is very easy to configure and allows generating types by running npm command.
How To use Typescript Types for REST API
Even if you’re a fan of REST APIs and prefer not to rely on any tooling for documentation, I have a step-by-step guide to type your REST API with ease. If you use Express, you can use @types/express and extend its Request and Response types. You can type query parameters, body parameters and responses for your API.
The goal is to import all created types into a single file. It’s better to organize node.js code into modules where each module has
types.ts with all module’s types. This way you can import all types into a single file.
Now, we need to be able to export all types into a single file. We can achieve this by creating a separate Typescript configuration file that we use only for generating our types for another projects. The key is outFile option in Typescript configuration.
Then, we just add npm command in our Node.js project that exports types into single file and copy them into the front-end or whatever project.
In this blog post, we’ve explored a hassle-free method to share Typescript types between React and Node.js projects. Remember, organizing your Node.js modules efficiently is key to maintaining a clean and maintainable codebase. Embrace the power of Typescript to build robust and scalable applications 🙂
Happy coding, and may your projects flourish with Typescript success!