Master The Art Of Using Typescript Without The Compile Step by@theBenForce

Master The Art Of Using Typescript Without The Compile Step

With just a few tricks you can start writing your scripts in TypeScript. The only magic that's required is the first line needs to be a shebang statement. Set up your environment and globally install node and npm packages to create your script. Then mark the script as executable and use bash to execute the contents of the file. You can now directly execute your typescript file using bash or node-node-generated-script. Using bash to run your TypeScript script is easy to do with bash.
image
Ben Force HackerNoon profile picture

Ben Force

4x AWS Certified Senior Software Engineer. I write about architecture, infrastructure as code, and automation.

linkedin social icontwitter social iconinstagram social icongithub social icon

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

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!

image

Previously published at https://justwriteapps.com/running-typescript-without-compiling/

react to story with heart
react to story with light
react to story with boat
react to story with money
Ben Force HackerNoon profile picture
by Ben Force @theBenForce.4x AWS Certified Senior Software Engineer. I write about architecture, infrastructure as code, and automation.
Visit My Blog
L O A D I N G
. . . comments & more!