All academies
Roadmap/Fundamentals

Level 1

Introduction to TypeScript

Learn what TypeScript is, why it exists, and how it relates to the JavaScript you already know. You will see your first type annotations and understand what the compiler does (and does not do) for you.

15 min read + quiz

What Is TypeScript?

TypeScript is a programming language built on top of JavaScript. Every valid JavaScript program is also a valid TypeScript program, which is why people describe TypeScript as a 'superset' of JavaScript. What TypeScript adds is a static type system: a way to describe the shapes of your values (numbers, strings, objects, functions) so that a tool can check your code for mistakes before it ever runs.

The key word is 'before'. JavaScript only discovers problems at runtime, when a user is already clicking around your app. TypeScript moves a whole category of errors, like calling a function with the wrong argument or misspelling a property name, to compile time, where they show up as red squiggles in your editor instead of bug reports from users.

TypeScript was created at Microsoft and released in 2012, and it has since become the default choice for most professional JavaScript projects. Frameworks like Angular are written in it, and React, Vue, and Node.js codebases overwhelmingly adopt it because it makes large codebases easier to navigate, refactor, and trust.

// JavaScript: this bug hides until runtime
// TypeScript: the compiler flags it immediately

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

greet("Ada");     // OK
// greet(42);     // Error: Argument of type 'number' is not
                  // assignable to parameter of type 'string'.

A type annotation on the parameter turns a silent runtime bug into an immediate compile-time error.

Why Static Types Matter

Consider a plain JavaScript function that formats a price. If someone passes a string instead of a number, JavaScript will happily concatenate instead of adding, and you will ship a page that says your product costs '19.99USD undefined'. Nothing crashes; the output is just wrong. These 'silently wrong' bugs are the hardest kind to find.

Static types act like a contract. When you write 'price: number', you are telling both the compiler and every future reader of your code exactly what this function expects. The compiler then enforces the contract at every call site, across your entire codebase, every time you save a file. Humans forget contracts; compilers never do.

Types also power your editor. Autocomplete, jump-to-definition, inline documentation, and safe automated renames all come from the type system. Even developers who claim not to care about type safety benefit from this every day: when you type a dot after a variable and see a list of its properties, that is the type system working for you.

function formatPrice(price: number, currency: string): string {
  return price.toFixed(2) + " " + currency;
}

console.log(formatPrice(19.99, "USD")); // "19.99 USD"

// formatPrice("19.99", "USD");
// Error: Argument of type 'string' is not assignable
// to parameter of type 'number'.

The contract is explicit: price must be a number, so .toFixed() is guaranteed to exist.

TypeScript Is JavaScript, Plus Types

It helps to think of TypeScript as two separate things bundled together: the JavaScript language you already know, and a type layer that lives alongside it. When TypeScript compiles your code, it performs 'type erasure': every annotation, interface, and type alias is simply deleted, and what remains is plain JavaScript.

This has an important consequence that trips up many beginners: types do not exist at runtime. You cannot check 'if (x is MyInterface)' the way you might in Java or C#, because by the time the code runs, MyInterface is gone. Runtime checks still have to be written in ordinary JavaScript, using typeof, instanceof, or your own validation logic.

The upside of erasure is that TypeScript adds zero runtime cost. Your compiled output is just the JavaScript you would have written anyway, so there is no performance penalty and no special runtime library to ship to users.

// What you write (TypeScript):
const age: number = 30;

interface User {
  name: string;
}

const user: User = { name: "Grace" };

// What actually runs (after compilation, plain JavaScript):
// const age = 30;
// const user = { name: "Grace" };
// The interface vanished entirely - types are erased.

After compilation, all type information is erased. Only ordinary JavaScript remains.

The Compiler and Your Editor

The TypeScript compiler is a command-line tool called tsc. It reads your .ts files, checks the types, reports any errors, and emits .js files. In most modern projects you rarely run tsc by hand; your bundler or framework runs it for you, and your job is simply to keep the error count at zero.

Just as important is the TypeScript language server, which runs inside editors like VS Code and Cursor. It is the same checker as tsc, but running continuously as you type. Every red squiggle, every autocomplete suggestion, and every hover tooltip comes from it. This tight feedback loop is where TypeScript delivers most of its day-to-day value.

One subtlety worth knowing early: type errors do not necessarily stop your code from running. TypeScript can still emit JavaScript even when there are errors (this is configurable). Type checking is a quality gate, not a hard barrier, though most teams treat any error as a build failure.

What TypeScript Does Not Do

TypeScript does not validate data at runtime. If your server sends malformed JSON, TypeScript cannot catch it, because the types were erased before the code ever ran. A common beginner mistake is writing 'const user = response as User' and believing the data is now guaranteed to match; in reality that assertion is just you telling the compiler to trust you.

TypeScript also does not change how JavaScript behaves. Quirks like floating-point arithmetic, type coercion with the == operator, and 'this' binding all work exactly as they do in JavaScript. TypeScript can warn you away from some of these traps, but it does not remove them.

Finally, TypeScript cannot prove your program is correct, only that it is type-consistent. A function can have perfect types and still contain a logic bug, like subtracting where you meant to add. Types are a powerful safety net, not a substitute for tests and careful thinking.

interface User {
  name: string;
  email: string;
}

// DANGER: 'as' does not check anything at runtime.
const data: unknown = JSON.parse('{"name": "Eve"}');
const user = data as User;

console.log(user.email.toUpperCase());
// Compiles fine, crashes at runtime: email is undefined.

Type assertions bypass checking. Data from the outside world still needs runtime validation.

Try it yourself

This lesson's example is preloaded in the playground — break it, fix it, extend it.

Open in playground

Check your understanding

Question 1 · Multiple choice

What is the relationship between TypeScript and JavaScript?

Question 2 · True or false

TypeScript types are available at runtime, so you can check whether an object matches an interface while the program is running.

Question 3 · Predict the output

What does this program print?

function label(value: number): string {
  return "Value: " + value.toFixed(1);
}

console.log(label(3.14159));

Question 4 · Find the error

This code fails to compile. Which line is the problem?

function double(n: number): number {
  return n * 2;
}

const a = double(21);
const b = double("21");
console.log(a, b);

Question 5 · Multiple choice

A teammate writes: const user = JSON.parse(input) as User. What protection does this give at runtime?