Skip to lesson

TypeScript · Lesson 2 of 42

Installing TypeScript and running your first program

Install Node.js and the TypeScript compiler, create a project, write hello.ts, compile it to JavaScript and run it from the terminal.

Beginner to advanced·12 min·Getting started·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will have a working TypeScript setup on your computer, you will have compiled and run a program, and you will know which command does what. This is the only lesson in the course that is mostly about setup, and after it you can focus on the language.

What you need

You need two things: a text editor and Node.js. Node.js runs JavaScript outside the browser and comes with npm, the package manager used to install the compiler. Install the current long-term support version from the official Node.js website. To check, open a terminal and run:

bash
node --version
npm --version

Each command prints a version number. If you see "command not found", the installation did not finish or the terminal needs to be reopened.

For an editor, Visual Studio Code has TypeScript support built in, and most other popular editors support it through a plugin. In an editor, errors appear as red underlines while you type, before you run anything.

Create a project

Make a folder for the course and set it up as an npm project:

bash
mkdir ts-course
cd ts-course
npm init -y
npm install --save-dev typescript

The second command creates a package.json file. The last one downloads the TypeScript compiler into a node_modules folder and records it in package.json as a development dependency. Installing it in the project, instead of only on your computer, means anyone else who clones the project gets the same compiler version when they run npm install.

Check that the compiler works:

bash
npx tsc --version

npx runs a program from your project's node_modules. The command prints Version followed by the compiler's version number. Because npm installs the latest release, the number depends on when you install. The samples in this course were checked with TypeScript 5.9 and 7.0. A few long messages, mostly ones that list several overloads, are worded slightly differently between versions, so your output may differ a little from the text shown here.

Your first program

Create a file named hello.ts and put this in it:

typescript
function greet(name: string): string {
  return "Hello, " + name + "!";
}

console.log(greet("TypeScript"));
result
Hello, TypeScript!

The only thing that is not plain JavaScript is : string twice. The first says the parameter name must be a string. The second, after the parentheses, says the function returns a string.

Compile it:

bash
npx tsc hello.ts

If there are no errors, the command prints nothing and creates a new file, hello.js, beside hello.ts. When you name a file like this, tsc ignores any tsconfig.json and uses its default options. Whether strict is among those defaults depends on the compiler version, so add --strict (npx tsc --strict hello.ts) if you want the strict checks this course assumes. Run the generated file with Node.js:

bash
node hello.js
result
Hello, TypeScript!

You have just done the full cycle: write TypeScript, compile to JavaScript, run the JavaScript.

Seeing an error

Change the call to greet(42) and compile again. The compiler stops you with a message, as the lesson on compiler errors explains in detail:

typescript
function greet(name: string): string {
  return "Hello, " + name + "!";
}

console.log(greet(42));
result
example.ts(5,19): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

A configuration file

Typing the file name every time gets tedious, and real projects have many files. The compiler reads a settings file named tsconfig.json. Create one with:

bash
npx tsc --init

This writes a tsconfig.json with explanatory comments. From now on, running npx tsc with no file name compiles every TypeScript file the configuration includes. You will learn the important options in the lesson on tsconfig.json and strict mode. For now, leave the defaults.

Open the generated file and look at it. Recent versions of tsc --init turn on strict, which this course assumes. They also turn on a few extra checks (noUncheckedIndexedAccess and exactOptionalPropertyTypes), and they write extra output files such as .d.ts and .map files next to your JavaScript. With the extra checks on, a sample may report one more error than the text shows. They also set types to an empty list, which matters when you use Node.js APIs such as process: the lesson on declaration files explains how to add @types/node.

Other ways to run TypeScript

Compiling and then running is the clearest way to learn, so this course uses it. You may also meet tools that compile and run in one step, and recent versions of Node.js can run some TypeScript files directly by removing the types. These are convenient, but they usually skip type checking, so they are a complement to running tsc, not a replacement. Check the documentation of the version you use for the details.

Common mistakes

Running the .ts file with node. node hello.ts is not the workflow described here. Compile first with tsc, then run the .js file.

Installing the compiler but running the wrong one. If tsc is not found, use npx tsc, which finds the copy installed in your project.

Editing the .js file. The .js file is generated. Your next compile overwrites it, so make changes in the .ts file.

Ignoring red underlines. The editor and the compiler report the same problems. Fix them in the editor and compilation will usually pass.

Exercise

Set up the project as above. Write hello.ts with a greet function, compile it and run it. Then try the four calls you predicted at the end of the last lesson on the addTax function: compile each one and check whether the compiler agrees with you. Keep any call that fails and read the message; the next two lessons help you understand it.

Next: How the TypeScript compiler works: checking and emitting.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 Which command adds the TypeScript compiler to a project as a development dependency?
02 What does npx tsc --init create?
03 After tsc compiles hello.ts, which file do you run with Node.js?
04 Why install TypeScript in the project instead of only globally?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Frontend

TypeScript tutorial

Add types to JavaScript to catch mistakes before your code runs, from your first annotation to generics and real projects.

Your progress 0/42
Learning something new?Explore all courses