TypeScript conquers the command line: The end of Bash?

Creating a CLI application with TypeScript – what sounds like a crazy idea at first, in practice offers advantages over classic bash scripts.

listen Print view
Hands on laptop keyboard with blurred code in the background

(Image: Tero Vesalainen/Shutterstock.com)

16 min. read
By
  • Timo Zander
Contents

JavaScript and its type-safe sister language TypeScript seem to be unstoppable in their popularity. Whether in front-end or back-end development, the languages are now the first choice of many developers. It is therefore not surprising that developers are increasingly creating CLI scripts – typically written in Bash, PowerShell or Python – using TypeScript.

Timo Zander
Timo Zander

Timo Zander studied Applied Mathematics and Computer Science and works as a software developer. He is interested in open source, the JavaScript universe and emerging technologies.

Using a web scripting language to create shell applications seems absurd. However, thanks to the Node runtime environment, TypeScript is platform-independent, widely used and has an extensive standard library. The ecosystem also includes many packages that simplify the creation of CLI applications. The barrier to entry is therefore low – in contrast to Bash, a scripting language that is repeatedly criticized for its peculiarities, difficulty and unreadable syntax.

Larger software projects often use individual scripts for their workflow: for example, for setting up the environment, for the build or for generating code. If these projects are written in JavaScript or TypeScript, such scripts are increasingly being written in the same language. This means that developers do not have to switch between different languages and contexts and can also transfer their knowledge of JavaScript to the necessary CLI scripts. Projects such as Pulumi, an open source tool for infrastructure as code, underline these advantages: Instead of learning their own languages used only for this purpose, developers can use Pulumi to write their infrastructure-as-code using their favorite general-purpose language.

At the same time, scripts written in JavaScript are platform-independent. Almost all developers have Node.js or a Node.js-compatible runtime environment on their computer, whether on Windows, Mac or Linux. A Bash script, on the other hand, causes problems for Windows users and requires tools such as MinGW (a port of the developer tools GNU Compiler Collection and GNU Debugger for Windows) or the Windows Subsystem for Linux (WSL).

Alternatively, the sister language TypeScript can also be used. The Node.js competitors Deno and Bun even support the execution of TypeScript code natively – and Node.js has also had built-in support for TypeScript since version 22.7, although this is still experimental. Packages such as tsx or ts-node help to make a TypeScript CLI application available to a broad target group. These combine the build and execution steps so that TypeScript code can be executed using a single command. The npx tool, which is included with the Node.js installation, can call these packages without manual installation. A script can be executed via npx tsx ./script.ts. Or the file can be provided with a shebang (#!) (Listing 1) so that it can be executed directly (via ./script.ts).

Videos by heise

Listing 1: A .ts file can be executed directly with the help of a shebang

#!/usr/bin/env -S npx tsx
import { Command } from "@commander-js/extra-typings";
// ...

This shebang signals that this script will be executed using the npx tsx command. In addition, the file requires execution rights and there must be a Node.js installation on the computer so that npx is installed and available in the environment. However, not all Unix or Unix-like systems allow arguments in the shebang: in these cases, execution is somewhat more complicated.

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.