Skip to Content
TypeScript for Beginners: Catch Wrong Data Before It Runs
Beginner
0 likes
Courses
TypeScript for Beginners: Catch Wrong Data Before It Runs

Install TypeScript and Run Your First .ts File

Install the compiler in a project, compile a .ts file, run the result and read the first error. After this, you have a working setup for every example in the course.

Course content

Sign in to save your progress.

Course Home

Skip ahead: If npx tsc --version already prints a version in a project folder, skip to "Compile your first .ts file", then continue.

Nothing in this course works until the compiler is on your machine and you have seen it run once. Do it now, in a folder you can throw away.

Step 1: check Node

TypeScript is installed with npm, which comes with Node. If you have not installed Node yet, follow Install Node and Check the Version first.

node -v
npm -v

I used Node 24.21.0 and npm 11.19.0 for everything in this course. Yours may be newer. If something later does not match, check that first.

Step 2: make a project and install the compiler

mkdir ts-first
cd ts-first
npm init -y
npm install --save-dev typescript

The last command prints something like this. The number of packages and the time will differ on your machine.

added 2 packages, and audited 3 packages in 681ms

found 0 vulnerabilities

--save-dev records TypeScript in package.json as a development tool. It goes into the project, not your whole computer, so everyone who clones the project gets the same version. This course uses TypeScript 7.0.2.

npx tsc --version
Version 7.0.2

⚠ Warning: You can also run npm install -g typescript and have tsc everywhere. It works, but then your project depends on whichever version happens to be on your machine. Use the project install and call it with npx.

Compile your first .ts file

Create a file named hello.ts in the ts-first folder.

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

console.log(greet("Ana"));

Now compile it and run the result.

npx tsc hello.ts
node hello.js
Hello, Ana

tsc printed nothing, and that is the good outcome. It wrote hello.js next to your file and Node ran that. Open hello.js and compare. The : string parts are gone, and "use strict"; was added at the top. Your types never reach Node.

Break it on purpose

Change the last line of hello.ts so it passes a number.

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

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

Read it from left to right: file, line and column, error code, message. Line 5, column 19 is the 42. The message says exactly what is wrong. Reading the message is the skill, and it is cheaper than guessing.

Now look in the folder. hello.js was written anyway, and node hello.js prints Hello, 42. By default tsc reports the error and still emits the file. It also exits with a non-zero code, which matters when a script or a build server runs it. If you want no output on errors, there is a setting for it, and we meet it in the tsconfig.json lesson.

💡 Tip: To check without writing any files, run npx tsc --noEmit hello.ts. Use it when you only want the checking.

Common mistakes

  • Running node hello.ts or opening the .ts file in a browser. Run the compiled .js file.
  • Editing hello.ts, then running the old hello.js without compiling again.
  • Ignoring the error because the program still ran. It ran because JavaScript does not check anything. That is the problem you are fixing.

If you skip this setup and paste code into an online playground only, you will never see the .js file or the exit code, and the next lessons on tsconfig.json will make little sense. Which would you rather debug: a compiler message on line 5, or a wrong value in a report next week?

About

Install the compiler in a project, compile a .ts file, run the result and read the first error. After this, you have a working setup for every example in the course.

Duration
5 min
Last update
10/10/2026
Was this helpful?
​ 0 ​ 0

Comments (0)

Sign in to comment

No comments yet.