Skip to main content

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:

Bash
mkdir -p ~/hello-ts
cd ~/hello-ts
npm init -y
npm install --save-dev typescript@7.0.2
npx tsc --version

Line by line:

  • npm init -y writes a package.json with default values. npm needs it to record what the folder depends on.
  • npm install --save-dev typescript@7.0.2 downloads the compiler into node_modules/ and lists it under devDependencies: a tool you use while writing code, not something the finished program needs. The @7.0.2 pins the exact release this page was written against.
  • npx tsc --version runs the tsc program from this folder's node_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:

Bash
cat > hello.ts <<'TS'
function greet(name: string): string {
  return `Hello, ${name}`;
}

const message: string = greet("world");
console.log(message);
TS

What each line does:

  • function greet(name: string): string { declares a function called greet. The name: string part is a type annotation: whoever calls greet must pass text. The : string after the brackets says the function returns text.
  • ` return `Hello, ${name}; ` builds the greeting. The backticks make a template string, and `${name} inserts the value of name. This line is plain JavaScript.
  • const message: string = greet("world"); calls the function and stores the result. The : string here is optional, because TypeScript can work out the type from what greet returns; 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:

Bash
npx tsc hello.ts
cat hello.js
node hello.js

npx 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:

JavaScript
"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:

Bash
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:

Text
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 1

Read 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:

Bash
node hello.ts
node oops.ts

Check 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 tsc and getting command not found. The compiler is installed in the folder, not globally. Use npx tsc from inside the folder, or add a script to package.json.
  • Believing node file.ts checked your types. It only strips them, as you saw with oops.ts. Run npx tsc --noEmit to check.
  • Shipping JavaScript from a failed build. tsc writes 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 annotation x: string is 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:

Bash
cd ~
rm -rf ~/hello-ts

Check it worked

ls ~/hello-ts now reports No such file or directory.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.