Sub topic 1
Setting Up a TypeScript Command-Line Project
A command-line application runs from a terminal instead of a graphical interface. In this tutorial, you will build wcl, a small tool that counts lines and words in a file.
Prerequisites
Assume you know basic JavaScript or TypeScript syntax, including functions, object types, and async/await. You also need Node.js and npm installed. Use Node.js 20 or a newer LTS release. You should be comfortable running commands in a terminal and reading a basic package.json file.
The project will use TypeScript source files in src and compiled JavaScript files in dist.
Creating the project
Create a directory and initialize it as an npm package:
mkdir wcl
cd wcl
npm init -y
npm install --save-dev typescript @types/node
mkdir srcThe typescript package provides the compiler. The @types/node package provides TypeScript declarations for Node.js modules such as node:fs/promises and node:util.
Use this layout:
wcl/
├── package.json
├── tsconfig.json
└── src/Keep source and generated files separate. This makes it clear which files you edit and which files the compiler creates.
Configuring the TypeScript compiler
Create tsconfig.json with these settings:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"outDir": "dist",
"rootDir": "src",
"types": ["node"]
},
"include": ["src"]
}The target setting chooses the JavaScript language version that TypeScript produces. ES2022 works well with current Node.js releases.
module and moduleResolution use Node's modern ECMAScript module rules. With these settings, TypeScript understands imports and exports as Node.js uses them. The strict option enables type checks that catch many mistakes during compilation.
rootDir identifies your TypeScript source directory. outDir identifies the directory for compiled JavaScript. The include setting limits compilation to files under src.
Declaring the package
Replace package.json with this content:
{
"name": "wcl",
"version": "1.0.0",
"type": "module",
"bin": {
"wcl": "dist/cli.js"
},
"files": ["dist"],
"scripts": {
"build": "tsc",
"test": "tsc && node --test dist"
},
"devDependencies": {
"@types/node": "^22.0.0",
"typescript": "^5.0.0"
}
}The type field tells Node.js to treat .js files as ECMAScript modules. The bin field will later expose the compiled dist/cli.js file as the wcl command. The files field limits published package contents to the compiled output.
Your installed dependency versions may differ from the example. npm records the exact versions in package-lock.json, so commit that file with the project.
Checking the setup
Create a temporary source file to confirm that the compiler works:
// src/cli.ts
console.log("wcl is ready");Run the build:
npm run build
node dist/cli.jsThe command should print:
wcl is readyThe generated dist/cli.js is the file Node.js executes. Later, you will replace the temporary message with argument parsing and file-counting behavior.
Exercise
Exercise
Why does this project keep TypeScript files in src and compiled JavaScript files in dist?
Show answer
Separating source from generated output keeps editing predictable and lets the compiler recreate dist without overwriting source files.