Unlock the Power of TypeScript with tsm
The web development world has been revolutionized by TypeScript, and its popularity continues to soar among enterprise projects and solo developers alike. Traditionally, executing TypeScript code involves transpiling it down to vanilla JavaScript for use in common environments like Node.js or the browser. However, a few innovative projects have emerged that allow developers to run their TypeScript code directly, and tsm is one of them.
What is tsm?
tsm is a brand-new TypeScript module loader for Node.js, created by Luke Edwards. It expands Node.js to provide native TypeScript support without the need to transpile it to JavaScript first. By leveraging esbuild under the hood, tsm quickly transpiles TypeScript sources into JavaScript on the fly before passing it to the Node.js runtime. This approach abstracts away the typical transpilation step, making it transparent to the user.
The Advantages of Using tsm
- Faster Development Feedback Loops: With tsm, developers don’t need to worry about TypeScript transpilation, resulting in shorter development feedback loops.
- Simplified Build Tooling: tsm eliminates the need for complex build tooling, making it easier to set up and maintain projects.
- Easy Deployment: With tsm, deployment becomes more straightforward, as there’s no need to transpile code beforehand.
- Native Node.js Support: tsm runs on top of Node.js, making it easy for developers familiar with Node to incorporate TypeScript into their projects with a simple step.
- Source Map Support: tsm supports source maps, allowing developers to debug their TypeScript programs with the standard V8 debugging protocol and popular debugging tools like VS Code.
The Disadvantages of Using tsm
One relatively small trade-off of using tsm is that it must be installed, in addition to Node.js itself, on the system that needs to run the TypeScript code. However, this is not a significant issue for fully packaged Node.js modules. Moreover, unlike other elements of a project’s technology stack, JavaScript build tools like tsm can be swapped out later with minimal impact on the application’s source code itself.
How to Use tsm in a Node.js Project
There are several ways to integrate tsm into a Node.js project:
tsm my-ts-file.ts
or
node -r tsm my-ts-file.ts
or
node --loader tsm my-ts-file.ts
You can also use tsm as a replacement for the node executable or use it with a shebang interpreter directive.
Deno: An Alternative to tsm
tsm isn’t the only way to achieve transpilation-less executable TypeScript. Deno, a project created by Ryan Dahl, the founder of Node.js, ships with TypeScript support out of the box. Deno provides its own standard library as a replacement for the Node.js APIs and has its own paradigm for distributing modules. While code written for Node.js in TypeScript may not be portable to Deno, it’s an alternative worth considering.
Real-World Examples of tsm in Action
Several notable projects have already adopted tsm, including:
- create-figma-plugin, a comprehensive toolkit for developing plugins and widgets for Figma and FigJam;
- tinyhttp, a modern web framework written in TypeScript;
- nanostores, a tiny state manager for JavaScript projects.
Get started with tsm today and discover the benefits of native TypeScript support in your Node.js projects!