Pirobits
Blog@bypirob

Run TypeScript code with TSX (TS-NODE alternative)

alberto avatar
topic
Alberto Sola · 7/22/2024 · 3 min

Node is one of the most practical runtimes available today, and I often use it for certain tasks (alongside Python or Go for others). In general, I enjoy working with TypeScript because having a type system helps me structure the code better, in addition to helping you see where changes impact.

In this post, I want to talk about the tool tsx which allows you to execute TypeScript code, as an alternative to ts-node, which I have always used.

I am not a TypeScript purist, and if there are types that are excessively complicated, I prefer to avoid wasting time defining and maintaining these cases.

As you may already know, TypeScript is a superset of JavaScript that primarily allows you to define types. The problem is that Node (unlike Deno) does not natively support TypeScript code, so you have to first transpile TypeScript to JavaScript to be able to execute it.

The most famous compiler is tsc, although there are others like swc (written in Rust) or esbuild (written in Go), which allow you to transpile the code so that it translates to JavaScript with which you can then execute your code with Node. There is a utility that I have always used for local development, which is ts-node, that allows you to skip these steps and directly execute ts-node my-program.tsx to simplify the development process.

Although I have always used this tool, I wondered if there was any alternative due to the configuration issues it sometimes gave me. This week I developed a small utility to send emails with Amazon SES and took the opportunity to explore other options.

While reading about this, I discovered tsx, a tool based on esbuild for transpiling code. What I love about tsx is that it does not perform type-checking, making it much faster. Additionally, its configuration is straightforward: install and run without complications.

Installing and trying it out is really easy. In the terminal, in your project's working directory, run the following:

npm i -D tsx
npx tsx file.tsx

Remember that you can always add a shortcut to the scripts section of the package.json file.

I have been testing it this week and I really like it. It works efficiently and quickly. The idea of not checking types at compile time is good since the IDE you use, such as Visual Studio Code, already includes a server that handles type-checking while linting your code. And if you need additional checking, for example, to deploy your code to production, you can always run tsc or another tool that verifies the types both locally and in the pipeline (CI/CD).

If you found this article useful I would appreciate if you subscribe to my newsletter. You will receive exclusive quality content and you will also help me enormously. Each subscription supports the work I do and allows me to learn more about the topics you are interested in, so that I can improve the knowledge I share with you.


Recent posts