Ben Force
Ben Force - 5x AWS Certified Senior Software Engineer.
Published 1 min read

Running TypeScript without Compiling

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.

ℹ️ Since writing this article I’ve switched from using ts-node to TypeScript Execute. You can read more about it in My Node.js Toolkit for DevOps Scripting.

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

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

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!");

Now mark the script as executable.

chmod +x ./test.ts

And now you can directly execute your typescript file!

Shell Recording


Related Posts

Optimizing Nest-Commander: User Inputs

Optimizing Nest-Commander: User Inputs
Published 4 min read

Learn how to boost the functionality of your Nest-Commander application by incorporating user inputs effectively. Discover essential tips and techniques for adding inputs to enhance user interaction and command versatility.