EXAMPLE 1: Echo command-line input
console.log(process.argv);
EXAMPLE 2: Count duplicates in a file import { readFile } from "fs/promises";
readFile("0.txt", "utf-8").then((text) => {
const frequency: { [line: string]: number } = {};
text
.split("\n")
.forEach((line) => (frequency[line] = frequency[line] + 1 || 1));
Object.entries(frequency)
.filter(([key, value]) => value > 1)
.forEach(([key, value]) => console.log(value, key));
});
EXAMPLE 3: GET request fetch(process.argv[2]).then(console.log).catch(console.error);
EXAMPLE 4: GET parallel requests process.argv.slice(2).map((url) => {
const start = Date.now();
fetch(url).then((res) =>
console.log(
`${(Date.now() - start) / 1000}s`,
res.headers.get("Content-Length"),
url
)
);
});
Using the author's criteria, relative to Tcl, these implementations (to me) are:1. Significantly shorter, just as expressive, and familiar to millions of (JS/TS) devs
2. Fast, owing to Node.js/V8's insane optimization level
3. Strongly typed
But you know what? I don't care much about terseness or speed when writing scripts. Honestly, the real reason I use TS for scripts is simple: comfort. I'm comfortable and productive with it. Everything else is just post-hoc rationalization. I'd wager it's the same story for many of these "Why <language/tech>?" posts.
It's perfectly fine to use something simply because you like it -- you're in good company. :)