DEV Community

Cover image for Running TypeScript without Compiling
Ben Force
Ben Force

Posted on • Originally published at justwriteapps.com on

9 2

Running TypeScript without Compiling

If you're like me you create scripts to automate things all the time. While you can do quite a bit with bash, it's just a lot easier to use your primary language--in this case TypeScript. With just a few tricks you can start writing your scripts in TypeScript.

Setup your Environment

First you need to get your environment setup. I'm going to assume you already have node and npm installed and your globally installed packages are in your path. Once you have that setup, globally install ts-node.



npm i -g ts-node


Enter fullscreen mode Exit fullscreen mode
Globally installing ts-node

Create the Script

If you've made it this far then it's time to create your script. The only magic that's required is the first line needs to be a shebang statement.



#!/usr/bin/env ts-node


Enter fullscreen mode Exit fullscreen mode

This tells bash that it should use ts-node to execute the contents of the file. Here's my sample script.



#!/usr/bin/env ts-node

function test(message: string) {
    console.info(message);
}

test("Hello TypeScript!");



Enter fullscreen mode Exit fullscreen mode

Now mark the script as executable.



chmod +x ./test.ts


Enter fullscreen mode Exit fullscreen mode

And now you can directly execute your typescript file!

Running TypeScript without Compiling

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay