Skip to content
Educora
Beginner14 min1 / 8

Why TypeScript?

Find out what TypeScript adds to JavaScript, how the compiler catches bugs before the code runs, and how to set up a project with `tsc`, `tsconfig.json` and Node.js.

Check yourself
In this lesson you will learn
  • Explain what static types are and which bugs they catch in advance
  • Describe how TypeScript code turns into JavaScript
  • Set up a project with tsc and a basic tsconfig.json
  • Run .ts files directly with Node.js or tsx and check the types with tsc --noEmit

Imagine an online shop where a function formatPrice(price) has worked perfectly for months. One day the price arrives from a form as the string '4.5' instead of a number, and the page breaks with TypeError: price.toFixed is not a function. The customer saw the bug before the developer did. TypeScript exists so that you, not your users, find such bugs — right in your editor, the moment you type them.

What is TypeScript?

Definition
TypeScript

A programming language created by Microsoft that adds static types to JavaScript. Every valid JavaScript program is also a TypeScript program; TypeScript simply lets you describe what kinds of values variables, parameters and functions hold.

In JavaScript, types are checked dynamically — only when a line actually runs. In TypeScript they are checked statically — by reading the code, without running it. You write the type after a colon; this is called a type annotation: price: number means “price is always a number”. If you pass a string anywhere, the compiler tells you at once:

TypeScript
function formatPrice(price: number): string {
  return price.toFixed(2) + ' AZN';
}

console.log(formatPrice(4.5));
console.log(formatPrice('4.5'));
: string says that the function returns a string. The second call is a mistake.
Text
shop.ts:6:25 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

6 console.log(formatPrice('4.5'));
                          ~~~~~
The compiler's message: file, line and column (shop.ts:6:25), the error code TS2345 and the reason. The wavy line marks the exact spot.

From TypeScript to JavaScript

Browsers and Node.js really understand only JavaScript. That is why the TypeScript compiler, tsc, does two jobs: it checks the types, and it emits ordinary JavaScript in which all the types are simply erased. Here is a small file and the JavaScript that tsc produces from it:

TypeScript
type User = { name: string; age: number };

export function greet(user: User): string {
  return `Hello, ${user.name}! You are ${user.age}.`;
}

const aysel: User = { name: 'Aysel', age: 14 };
console.log(greet(aysel));
Expected output
Hello, Aysel! You are 14.
greet.ts: type User describes the shape of the object.
JavaScript
export function greet(user) {
    return `Hello, ${user.name}! You are ${user.age}.`;
}
const aysel = { name: 'Aysel', age: 14 };
console.log(greet(aysel));
The result of tsc, greet.js: the type User line and all annotations are gone, while the logic is exactly the same.

What types give you

  • Bugs are caught early: a typo in a property name, a wrong argument or a forgotten null check is found before the program runs.
  • A smart editor: because the editor knows what every value is, autocompletion, go-to-definition and safe renaming across the whole project just work.
  • Types work as documentation: the header formatPrice(price: number): string tells you how to use the function without reading its body.
  • Safe changes in big projects: change a function's parameters and the compiler lists every place that has to be updated.

Setting up a project

  1. 1
    Install TypeScript

    In the project folder, run npm install --save-dev typescript. The compiler goes into node_modules, and you call it with npx tsc.

  2. 2
    Create the config file

    npx tsc --init creates a tsconfig.json file with recommended settings.

  3. 3
    Write the code

    Put your code in src/index.ts.

  4. 4
    Compile and run

    npx tsc checks the whole project and writes the JavaScript to dist/; then run it with node dist/index.js.

JSON
{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "strict": true,
    "rootDir": "src",
    "outDir": "dist"
  }
}
A minimal tsconfig.json. tsc --init generates more options, but these are the essentials.
OptionWhat it does
targetchooses which JavaScript version to emit, for example es2022
modulechooses the module system; nodenext follows the rules of modern Node.js
strictturns on the full set of strict checks — always keep it true
rootDir / outDirwhere the .ts sources live and where the .js output goes

Running .ts files directly

Modern Node.js (version 22.18 and newer, including Node.js 24) can run a .ts file directly: node index.ts. It uses type stripping: Node.js simply cuts the types out and runs the JavaScript that remains. The tsx tool does the same job in older versions of Node.js too.

Terminal
node src/index.ts      # Node.js 22.18+: strip the types and run
npx tsx src/index.ts   # tsx: run TypeScript on older Node.js too
npx tsc --noEmit       # only check the types, write no files
Two ways to run the code and one command to check the types.

Key points

  • TypeScript adds static types to JavaScript; bugs are found before the code runs.
  • A type annotation is written after a colon: price: number.
  • tsc checks the types and emits plain JavaScript with the types erased; at runtime there are no types.
  • Project settings live in tsconfig.json; strict: true should always be on.
  • node file.ts and tsx run the code but don't check types — that is what tsc --noEmit is for.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
What does TypeScript mainly add to JavaScript?