Hello World (first programs) · Beginner · 11 min
Hello World in TypeScript: catch a type error before it runs
By Padding · Updated 18 Nov 2026
node:26 image · Node.js 26.13 · TypeScript 7.0.2
You will install TypeScript 7 in a throwaway folder, compile and run a typed Hello World on Node.js 26, watch the compiler reject a wrong argument, and run the same file directly with Node's built-in type stripping.
On this page
TypeScript is JavaScript with type annotations. You write name: string to say a value must be text, and a compiler called tsc checks every use of that value before the program runs. Then it removes the annotations and writes ordinary JavaScript. The types never reach the running program: they exist only while you compile, which is why TypeScript can run anywhere JavaScript runs, in browsers and in Node.js alike.
TypeScript 7 is the first release of the compiler rewritten as a native program, which its announcement says is about ten times faster than before (Announcing TypeScript 7.0). The command and the language are the same as before. In this tutorial you write one small file, compile it, break it on purpose, and then run it a second way with Node.js alone. You need Node.js 26 and npm. On Ubuntu, follow the Node.js install tutorial; on Windows and macOS, use the LTS installer from nodejs.org.
TypeScript is common wherever a JavaScript codebase grows past what one person can hold in their head: large web front ends built with React, Angular or Vue, Node.js back ends and APIs, and npm libraries, which publish type definitions so editors can autocomplete them. The same checks also power the hints and red underlines in editors such as VS Code, even in plain JavaScript files.
Step 1 · Make a throwaway folder and install TypeScript
Create a folder for this tutorial, give it a package.json, and install the compiler into it:
mkdir -p ~/hello-ts
cd ~/hello-ts
npm init -y
npm install --save-dev typescript@7.0.2
npx tsc --versionLine by line:
npm init -ywrites apackage.jsonwith default values. npm needs it to record what the folder depends on.npm install --save-dev typescript@7.0.2downloads the compiler intonode_modules/and lists it underdevDependencies: a tool you use while writing code, not something the finished program needs. The@7.0.2pins the exact release this page was written against.npx tsc --versionruns thetscprogram from this folder'snode_modules/.bin, not a global copy, and prints its version.
Because the compiler is now a native program, npm also fetches a second package built for your operating system and processor. The download is tens of megabytes, so on prepaid mobile data run the install once and reuse the folder rather than repeating it.
Check it worked
npx tsc --version prints Version 7.0.2. package.json now contains a devDependencies section with "typescript": "^7.0.2".
Step 2 · Write hello.ts
Create the file with a typed function:
cat > hello.ts <<'TS'
function greet(name: string): string {
return `Hello, ${name}`;
}
const message: string = greet("world");
console.log(message);
TSWhat each line does:
function greet(name: string): string {declares a function calledgreet. Thename: stringpart is a type annotation: whoever callsgreetmust pass text. The: stringafter the brackets says the function returns text.` return `Hello, ${name};` builds the greeting. The backticks make a template string, and `${name}inserts the value ofname. This line is plain JavaScript.const message: string = greet("world");calls the function and stores the result. The: stringhere is optional, because TypeScript can work out the type from whatgreetreturns; it is written out so you can see where annotations go.console.log(message);prints the value. It is the same call you would write in JavaScript.
The cat > hello.ts <<'TS' line writes everything up to the closing TS into the file. You can also create hello.ts in your editor and paste the five lines of code.
Check it worked
Run cat hello.ts. It shows the five lines of code, with a blank line after the closing }.
Step 3 · Compile it and run the JavaScript
Hand the file to the compiler, then run what it produces:
npx tsc hello.ts
cat hello.js
node hello.jsnpx tsc hello.ts checks the file and writes hello.js next to it. It prints nothing when there are no errors. With a file name on the command line and no tsconfig.json in the folder, tsc uses its built-in defaults, which include strict checking. The real project settings live in tsconfig.json, which Set up a TypeScript 7 project walks through.
Check it worked
cat hello.js shows something like this:
"use strict";
function greet(name) {
return `Hello, ${name}`;
}
const message = greet("world");
console.log(message);Every : string has gone. That is type erasure: the output is the same program without the annotations, plus a "use strict" line because strict mode is on. node hello.js prints Hello, world. Node.js never saw a type.
Step 4 · Pass a number and read the error
Now make the mistake that TypeScript exists to catch. Write a second file that calls greet with a number:
cat > oops.ts <<'TS'
function greet(name: string): string {
return `Hello, ${name}`;
}
console.log(greet(42));
TS
npx tsc --noEmit oops.ts || echo "tsc exited with status $?"--noEmit tells the compiler to check only and write no JavaScript. The || echo ... part prints the exit status, because tsc exits with a non-zero status when it finds errors.
Check it worked
In a terminal, the compiler prints one error with a short code frame, then the status line. It looks something like this:
oops.ts:5:19 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
5 console.log(greet(42));
~~
Found 1 error in oops.ts:5
tsc exited with status 1Read the first line left to right. oops.ts:5:19 is the file, line 5, column 19: the 42, which the ~~ underlines in the copy of line 5 below it. TS2345 is the error code, which you can search for. The message says a number was given where the parameter name asked for a string. You found the bug without running the program. tsc uses this coloured, framed layout (its --pretty option) whenever it writes to a terminal. When its output goes to a file or another program, as in a CI log, it prints a plainer one-line form instead: oops.ts(5,19): error TS2345: .... The two forms carry the same information.
One detail surprises beginners. Without --noEmit, tsc reports the same error and still writes oops.js, because its default is to emit output even when types are wrong. Add --noEmitOnError if you want a failed check to produce no JavaScript at all.
Step 5 · Run TypeScript directly with Node.js
Node.js 26 can run a .ts file itself. It removes the type annotations as it loads the file, a feature the Node docs call type stripping (Node.js TypeScript docs). Try it on both files:
node hello.ts
node oops.tsCheck it worked
node hello.ts prints Hello, world. node oops.ts prints Hello, 42, with no error.
That second line is the point of this step. Type stripping deletes the annotations and runs what is left; it does not check them. The wrong argument went straight through, and JavaScript turned 42 into text. Node also ignores tsconfig.json, and it refuses TypeScript features that need code generated for them, such as enum. A common setup is to use node file.ts for quick runs and tsc --noEmit before you commit or deploy, so the checking still happens somewhere.
Step 6 · Review the common mistakes, then clean up
Before you delete the folder, go through the four mistakes that catch most people on their first day with TypeScript. You have now seen most of them happen:
- Running
tscand gettingcommand not found. The compiler is installed in the folder, not globally. Usenpx tscfrom inside the folder, or add a script topackage.json. - Believing
node file.tschecked your types. It only strips them, as you saw withoops.ts. Runnpx tsc --noEmitto check. - Shipping JavaScript from a failed build.
tscwrites output even when it reports errors. Treat a non-zero exit status as a failed build, or use--noEmitOnError. - Expecting types at run time. Code such as checking
typeof x === "string"is JavaScript and runs; the annotationx: stringis gone by then. Data from a form, a file or an API still needs checking in code, because the compiler never sees it. That includes multilingual text such as accented Yoruba or Igbo names, and phone numbers typed with spaces.
Then delete the folder. It holds the compiler, the two .ts files and the generated hello.js:
cd ~
rm -rf ~/hello-tsCheck it worked
ls ~/hello-ts now reports No such file or directory.
Next steps
- Turn this into a real project with a
tsconfig.json, asrc/folder and npm scripts: Set up a TypeScript 7 project. - See the plain JavaScript version of this first program and how Node.js runs it: Hello World in JavaScript.
- Try a runtime that checks and runs TypeScript with one tool: Hello World in Deno.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials