Compiling and Running TypeScript
- #typescript
- #tsc
- #node
- #npm-scripts
- #compiling
In questa lezione
1. The basic workflow
TypeScript files use the .ts extension. Once you have a project set up (see the previous lesson), the simplest possible workflow is:
- Create a file, e.g.
test.ts. - Write some TypeScript code in it.
- Compile it to JavaScript.
- Run the resulting JavaScript with Node.js.
// test.ts
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("world"));
2. Compiling with tsc
The TypeScript compiler, tsc, reads your .ts files, type-checks them, and emits plain .js files:
npx tsc test.ts
This produces a test.js file next to test.ts (or inside outDir, if your tsconfig.json defines one). You can then run the compiled output with Node.js like any regular JavaScript file:
node test.js
Nota
If you run npx tsc with no file argument, it compiles the whole project according to tsconfig.json instead of a single file.
3. Running .ts files more directly
Typing tsc and then node every time is tedious, so the ecosystem offers a few shortcuts:
3.1 Node’s built-in type stripping
Recent versions of Node.js can run .ts files directly, without a separate compile step, by stripping the type annotations at load time (it does not perform full type-checking — it just removes the types so the remaining code is valid JavaScript):
node --experimental-strip-types test.ts
In newer Node.js releases this behavior is being stabilized and may not require the flag at all — check your installed Node version’s documentation, since this feature is still evolving.
3.2 ts-node
ts-node is a popular community tool that compiles and executes TypeScript on the fly, with full type-checking:
npx ts-node test.ts
3.3 tsx
tsx is a faster, zero-config alternative built on esbuild, widely used for quick scripts and development servers:
npx tsx test.ts
Suggerimento
For learning and small scripts, any of these options is fine. For production builds, most teams still run a real tsc compile step (or a bundler like Vite/esbuild) so the shipped code is plain, already-checked JavaScript.
4. Automating with npm scripts
Typing long commands by hand gets old fast. package.json has a scripts section where you can define shortcuts:
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node test.ts"
}
}
Here a dev script was added that runs node test.ts (assuming your Node.js version supports running .ts files directly, per section 3.1 — otherwise point it at tsx test.ts or a tsc && node dist/test.js chain). Now, instead of remembering the exact command, you simply run:
npm run dev
npm run <script-name> looks up <script-name> inside package.json’s scripts object and executes it. This is the same mechanism behind commands you have probably already seen in other projects, like npm run build or npm run start.
4.1 A more complete example
A common pattern combines a type-check/build script with a dev script:
{
"scripts": {
"build": "tsc",
"start": "node dist/test.js",
"dev": "tsx watch test.ts"
}
}
npm run buildcompiles the whole project according totsconfig.json.npm run startruns the already-compiled output.npm run devre-runs the script automatically whenever the file changes (thanks totsx watch), which is convenient during development.
5. Further reading
- TypeScript Handbook — introduction
- TypeScript Playground — compile snippets instantly and inspect the emitted JavaScript
- Node.js type stripping — TypeScript blog
6. Quiz
Mettiti alla prova
0/6 risposte
What does
npx tsc test.tsdo?After compiling test.ts to test.js, how do you run the result?
What is a key limitation of Node's built-in type-stripping for .ts files?
What is the purpose of the
scriptssection in package.json?Which tool is described as a fast, zero-config alternative built on esbuild for running TypeScript directly?
If package.json defines
"dev": "node test.ts", how do you invoke it?
7. Exercises
Scenario: You want a smooth day-to-day workflow for a small TypeScript utility script, without typing long commands every time.
Task:
- Create
greet.tswith a typed function that takes aname: stringand returns a greeting string, and log the result. - Compile it manually with
npx tsc greet.tsand run the output withnode greet.js. - Add a
devscript topackage.jsonthat compiles and runs the file in one command (you can chain two commands with&&, e.g."dev": "tsc greet.ts && node greet.js"). - Run it via
npm run devand confirm it behaves the same as step 2. - Optionally, install
tsxas a dev dependency and add a second script,"dev:tsx": "tsx greet.ts", to compare the two approaches.
Learning objective: understand the difference between compiling and running TypeScript as separate steps versus using a direct runner, and practice wiring both into npm scripts.