← Learning Hub
Beginner

TypeScript Introduction

TypeScript is JavaScript with types. You write normal JavaScript, and add labels that say what kind of value each thing holds: a number, a string, a product object. TypeScript checks these labels before the code runs and points out mistakes in your editor.

In plain JavaScript, many mistakes - a misspelt property, a string where a number was expected, a missing argument - only show up when the code runs, sometimes in front of users. TypeScript finds them while you type. That's why most large JavaScript projects, and frameworks like Angular, use it.

The browser and Node.js don't run TypeScript directly. The TypeScript compiler (tsc) checks your code and turns it into plain JavaScript. Angular is written in TypeScript, and most big React and Node.js projects use it too.

Why use it?

  • Catches typos and wrong values while you type, not after users find the bug
  • Your editor can suggest property names and show what a function expects
  • Big projects are easier to change safely, because the compiler finds every place you broke

Install and run

Example
npm install -g typescript      # gives you the tsc command
tsc --version
# Version 5.8.3

tsc hello.ts                   # checks hello.ts and writes hello.js
node hello.js

npx tsx hello.ts               # or run a .ts file directly in one step

Your first TypeScript

Example
You can edit this code.

The part after the colon (: string) is the type annotation. If you later write price = "cheap", TypeScript stops you: Type 'string' is not assignable to type 'number'.

Types disappear when the code is compiled. They help you while writing; they don't slow the program down.
Key points
  • TypeScript = JavaScript + types, checked before running.
  • tsc compiles TypeScript to JavaScript.
  • Types disappear at runtime.

📝 Test yourself

1. What does TypeScript add to JavaScript?
2. Which command compiles a TypeScript file?
3. Fill in the type for a price:
let price: = 499;