Remote Software Engineer at Stripe and cellist based out of Ontario. Previously at GitLab. Fascinated with building usable, delightful software.
June 17, 2018 | 2 minutes to read
If you’re like me, most of your time writing bash scripts is spent Googling basic things like “how to loop in bash” while grumbling to yourself how easy this would be in a sane language like TypeScript.
As it turns out, you can write shell scripts using TypeScript, and it isn’t even that hard! Here’s how:
If you haven’t already, download and install node.
After node is installed, install both TypeScript and ts-node globally using npm
:
npm install typescript ts-node -g
Create a new .ts
file with a shebang as its first line that points to ts-node
:
#!/usr/bin/env ts-node
Then, add some TypeScript-y stuff:
#!/usr/bin/env ts-node
console.log('Hello from TypeScript!');
After saving your TypeScript file, you’ll need to update its permissions to allow it to be executed:
chmod u+x your-shell-script.ts
You can now run the script as you would any other command-line utility:
> ./your-shell-script.ts
…which should result in a friendly message in your terminal:
> ./my-shell-script
Hello from TypeScript!
That seemed a bit too easy - shouldn’t there be an intermediate build step in there somewhere? As a sanity check, update your .ts
file with something that shouldn’t compile:
#!/usr/bin/env ts-node
console.log('Hello from TypeScript!');
// TypeScript compiler error:
// Type '4' is not assignable to type 'string'.
var myStr: string = 4;
Rerunning your script will now result in something like this:
/Users/nathanfriend/.nvm/versions/node/v7.10.0/lib/node_modules/ts-node/src/index.ts:250
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
test.ts(5,5): error TS2322: Type '4' is not assignable to type 'string'.
at createTSError (/Users/nathanfriend/.nvm/versions/node/v7.10.0/lib/node_modules/ts-node/src/index.ts:250:12)
at getOutput (/Users/nathanfriend/.nvm/versions/node/v7.10.0/lib/node_modules/ts-node/src/index.ts:358:40)
at Object.compile (/Users/nathanfriend/.nvm/versions/node/v7.10.0/lib/node_modules/ts-node/src/index.ts:546:11)
at Module.m._compile (/Users/nathanfriend/.nvm/versions/node/v7.10.0/lib/node_modules/ts-node/src/index.ts:430:43)
at Module._extensions..js (module.js:580:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/nathanfriend/.nvm/versions/node/v7.10.0/lib/node_modules/ts-node/src/index.ts:433:12)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Function.Module.runMain (module.js:605:10)
This is good! You shouldn’t be able to run a TypeScript file if it contains compile-time errors.
There isn’t one! Well, not one that you have to explicitly run, anyway. This is the magic of ts-node - it compiles and runs TypeScript files on the fly much like regular node runs JavaScript files.
Other posts you may enjoy:
October 14, 2024 | 3 minutes to read
May 31, 2024 | 6 minutes to read
June 26, 2023 | 14 minutes to read
January 25, 2022 | 6 minutes to read
August 26, 2021 | 2 minutes to read