My TypeScript setup for Node.js

I want to say my setup is lean. But with TypeScript, a certain number of tools is unavoidable. I've reduced it to a minimum for my needs and will present the essential configurations.

3 minutes

Foundation

The foundation is the package manager npm. I haven't yet dared to switch to Bun, but this has been on my mind for some time. For the TypeScript configuration, I've ensured that I can declare my local imports absolutely. That is, instead of ../../../some/file, I use @/some/file. This simplifies refactoring and allows me to directly distinguish between my own and third-party content in the import list.

There are plenty of tweaks and compilation options in TsConfig. If you are interested into the details.

Just give me a call

Testing

Using TypeScript is also helpful for testing. To ensure that tests can handle absolute imports and apply them in the test folder, a corresponding configuration needs to be taken in the test tool – analogous to the TsConfig definition above. This is easily done with vitest… one of the reasons why I chose this tool.

API Definition

In my projects, I typically create a REST interface for communication with the frontend. On the one hand, I want to create an OpenAPI-compliant definition of the interface; on the other hand, I want that definition generated automatically from the code.

The latter is significant because experience shows that documentation and actual implementation can quickly diverge. This creates frustration and extra work for the team. The tool TSOA has proven effective for this. Automatic generation is based on TypeScript definitions. These need to be configured and integrated with absolute imports.

Linting

Besides typing, code formatting is essential. I would rather not have to worry about how my code is formatted. This should happen automatically and be ensured by appropriate tools. This facilitates team collaboration, as changes are made to programmatic adjustments rather than stylistic ones.

Bundling

As one of the final steps in the process, the TypeScript code needs to be bundled into a Node.js-compatible file. The package pkgroll has proven invaluable for this. It utilizes the existing configuration and doesn't require any additional files. This is a blessing, as all the tools mentioned above require separate configuration files, resulting in an extensive collection of configuration files at the repository root. This is definitely one of TypeScript's major drawbacks, which I didn't explicitly address in yesterday's article about why I start every project with TypeScript.

Conclusion

The various aspects discussed here highlight the TypeScript-specific setup. Beyond that, several other pieces of the puzzle affect the entire build and delivery pipeline. I've already touched on this with linting. I'll address other points, such as spell checking and semantic versioning, in future articles.

What does your TypeScript setup look like?

What do you consider essential in a TypeScript backend setup – and what have you deliberately removed?

I would love to share my experiences with you
call to action background image

Subscribe to my newsletter

Receive once a month news from the areas of software development and communication peppered with book and link recommendations.