Why TypeScript is the Ideal Language for Automation
Every developer has at least once spent hours manually renaming 200 files, collecting data from a dozen pages, or converting JSON to the required format. Routine kills productivity and motivation. But there's good news: automation in TypeScript solves these problems quickly and elegantly.
TypeScript is not just a superset of JavaScript. It offers strict typing, readable code, and the powerful Node.js ecosystem. If you write in Python but are looking for an alternative for automation, or already work with TS and want to get the most out of it — this article is for you. We'll cover real-world cases: from web page parsing to creating CLI utilities and task schedulers.
Getting Started: Setting Up the Environment
Before writing TypeScript scripts, you need to prepare the environment. Make sure you have Node.js (version 18+) and npm installed.
# Install TypeScript globallynpm install -g typescript
# Initialize a projectmkdir auto-ts && cd auto-tsnpm init -ynpm install typescript @types/node ts-node --save-devCreate a tsconfig.json file with basic settings:
{ "compilerOptions": { "target": "ES2020", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true }, "include": ["src/**/*"]}Now you can write scripts in the src folder and run them via ts-node src/script.ts. This is the foundation for any task automation ts.
Case 1: Automatic Web Page Parsing
Imagine: you need to collect news headlines from a news site's main page. Manual copy-pasting is a thing of the past. Let's write a parser in TypeScript using cheerio and axios.
npm install axios cheerioCreate a file src/parser.ts:
import axios from 'axios';import * as cheerio from 'cheerio';
interface NewsItem { title: string; link: string;}
async function parseNews(url: string): Promise<NewsItem[]> { const { data } = await axios.get(url); const $ = cheerio.load(data); const news: NewsItem[] = [];
$('h2.news-title a').each((i, el) => { news.push({ title: $(el).text().trim(), link: $(el).attr('href') || '', }); });
return news;}
(async () => { const items = await parseNews('https://example-news-site.com'); console.log(`Found ${items.length} news items:`); items.forEach(item => console.log(`- ${item.title}`));})();This parsing in TypeScript can be easily extended: add writing to CSV, sending to Telegram, or saving to a database. The NewsItem typing makes the code reliable and understandable.
Case 2: Batch File Processing
Suppose you have a folder with a hundred CSV files that need to be merged into one, removing duplicates. Manual work would take a day. Automation in TypeScript handles it in seconds.
npm install fs-extra papaparseimport * as fs from 'fs-extra';import * as path from 'path';import Papa from 'papaparse';
interface Row { [key: string]: string;}
async function mergeCSV(inputDir: string, outputFile: string): Promise<void> { const files = (await fs.readdir(inputDir)).filter(f => f.endsWith('.csv')); const allRows: Row[] = []; const seen = new Set<string>();
for (const file of files) { const content = await fs.readFile(path.join(inputDir, file), 'utf-8'); const parsed = Papa.parse<Row>(content, { header: true }); for (const row of parsed.data) { const key = JSON.stringify(row); if (!seen.has(key)) { seen.add(key); allRows.push(row);