Typescript sometimes has a reputation for being difficult to get started with. Modern tooling has really improved this situation and it is now very easy to create a new project and get up and running in no time. This post will cover how to setup a Typescript project quickly using and . We'll also cover some alternatives like and for running typescript files plus other runtimes like and . Let's go!

Create a new project (in 30 seconds)

In a terminal on Mac OSX or Linux create a new directory and navigate to it using this command:

Setup node

You need a file to run a typescript project. Use to create one:

The output shows a newly created file:

Install typescript

Next save compiler and the loader as development dependencies.

The shell output shows the installed dependencies (with your version instead)

Setup tsconfig

Next you can create a file using typescript's command:

A success message is shown in the terminal:

Create a typescript file

Now we can create a simple typescript file called

Run the script

You can run typescript using NodeJS with the loader. Run your script like so:

The output shows our message:

Next steps

You can now write and run typescript files easily. To make run commands easier try adding the scripts to the package.json file:

Then run your script using .

Alternative build tools

Running typescript with can sometimes be problematic (especially when mixing esm and cjs). It can be a good idea to consider other build tools like or for running typescript files.

SWC

SWC is a fast typescript and javascript compiler written in Rust 🦀. It is fast and can run Javascript and Typescript. Install it using npm:

Then run your typescript files using the loader:

Esbuild

Esbuild is another popular bundler that can compile Typescript. It is written in go and is very fast (but does not check types 😮!). Esbuild normally only handles compilation but you can use the package to run typescript files directly. Install it using npm:

Then run your typescript files using the loader:

Other runtimes

For those looking for a more modern runtime you can consider or . Both are modern runtimes that can run typescript files directly. They drop a lot of legacy baggage related to NodeJS but do come with their own trade-offs. Let's compare some briefly.

Deno

Deno was created by the original creator of NodeJS (Ryan Dahl) and is said to derive its name from "Node sorted":

That may be a bit tongue in cheek but Deno is a true modern alternative for running typescript and javascript. It has a built-in package manager and is designed to be more secure and modern than NodeJS. It also focuses on startup time and serverless environments.

Install it on Mac or Linux using the following command:

Or on windows:

Then to run a script simply type:

Deno has a modular permission system so if your script uses the web you can use the flag to enable network access.

Bun

Bun is another new runtime that is really fast and adds new features like macros and linting. It is written in Zig and uses JavaScriptCore instead of V8 which dramatically reduces startup time. It is a good choice for running typescript files and has a lot of potential for the future but might not be ready for production use yet.

To install on Linux or Mac:

For Windows:

Now to run a file type:

Why use Typescript?

Typescript is a superset of standard Javascript that adds a powerful type system to the Javascript you already know. It is totally optionally but can dramatically reduce compile-time bugs and exceptions due to the information that type systems can provide to a compiler and the process of compiling the typescript into javascript before running it.

This step provides an extra step of logic validation before running your javascript. TS-Node is a helpful tool for running the compilation step inside node itself so that you can skip the build stage. MailSlurp uses Typescript to create email testing APIs for NodeJS. Send and receive emails and attachments from test email accounts using MailSlurp for free!