TypeScript BasicsCore· 25 min read

What is TypeScript?

TypeScript is JavaScript plus types — it catches whole classes of bugs before your code ever runs.

What you will learn

  • Explain what TypeScript adds to JavaScript
  • See a bug TypeScript catches that JavaScript misses
  • Know that TS compiles down to plain JS

JavaScript, but with a safety net

TypeScript is a language built on top of JavaScript. Every piece of JavaScript you already know is still valid — TypeScript just lets you add types, little labels that say what kind of value something is (a number, a string, a list, and so on).

Those labels let a tool check your code before it runs and warn you when something does not add up — like a spell-checker for your data.

A bug JavaScript happily ignores

Imagine a function that should double a number. In plain JavaScript, nothing stops you from passing it text by mistake:

JavaScript runs this without complaint
// Plain JavaScript — no warning, wrong answer
function double(n) {
  return n * 2;
}

console.log(double(5));      // we meant to do this
console.log(double('5'));    // oops, a string slipped in

Note: Output: 10 10 Here both happen to print 10, because JavaScript quietly turns the string into a number. But double('5') + 1 elsewhere would give "51" — and JavaScript never warned you the input was the wrong type.

The same code in TypeScript

Now we add one tiny type label — n: number — to say "this must be a number":

TypeScript flags the wrong argument
// TypeScript — the mistake is caught instantly
function double(n: number): number {
  return n * 2;
}

console.log(double(5));      // fine
console.log(double('5'));    // red squiggle here, before running

Note: Output: Error: Argument of type 'string' is not assignable to parameter of type 'number'. TypeScript refuses to compile until you fix it. The bug is caught while you type — not by a confused user later.

Where does the JavaScript come from?

Browsers and Node (Node.js — a program that runs JavaScript on your computer, outside the browser) only understand JavaScript, not TypeScript. So TypeScript is compiled (translated) down to plain JavaScript first. You write .ts files; a tool turns them into .js files that run anywhere.

JavaScriptTypeScript
TypesOptional / noneYes, checked before running
Catches type bugs earlyNoYes
Runs in the browser directlyYesNo — compiles to JS first
Editor help (autocomplete)BasicExcellent

Tip: Big projects love TypeScript because the type checker catches mistakes that would otherwise become bugs. Angular is built with TypeScript, and most large React codebases use it too.

Q. What is the main thing TypeScript adds to JavaScript?

Answer: TypeScript is JavaScript plus a type system. It checks your types before running, catching many bugs early. It still compiles down to plain JavaScript.

✍️ Practice

  1. Write down two bugs you have hit in JavaScript that a "wrong type" warning could have caught.
  2. In one sentence, explain why a browser cannot run a .ts file directly.

🏠 Homework

  1. Find one popular tool or framework that uses TypeScript and note one reason its team chose it.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →