← TypeScript

Basic · 1 of 13 · 7 min read

What TypeScript is

A compile-time layer over JavaScript — what the compiler checks, what it erases, and the class of bug it cannot catch.

  • Type annotations

TypeScript is JavaScript plus a type system that runs before your code does. Every annotation you write is checked by the compiler and then deleted. What ships is plain JavaScript.

compile time runtime app.ts tsc type errors emits app.js no types left the engine runtime errors
Everything left of the line happens on your machine and in CI. Everything right of it happens in a browser or in Node, where the types no longer exist — which is why a bad API response is still a runtime error, however carefully the code is typed.

What the compiler does

// greet.ts — what you write
function greet(name: string): string {
  return `Hello, ${name}`;
}

greet(42); // Argument of type 'number' is not assignable to parameter of type 'string'
// greet.js — what is emitted, with the error fixed
function greet(name) {
  return `Hello, ${name}`;
}

The annotations are gone. There is no check in the output, no wrapper, no metadata — which is the whole design: TypeScript adds nothing to what you ship, and costs nothing at runtime.

What it catches, and what it cannot

Caught at compile time Not caught
A typo in a property name A property missing from a real API response
Calling a function with the wrong argument A null the server sent that your type did not mention
Forgetting a branch of a union A number that arrives as a string from a form
Using a value before it is assigned A library that changed its behaviour, not its types
A refactor that missed a call site Anything after as, any, or a !
interface User { id: number; name: string }

// The compiler believes you:
const user = JSON.parse('{"id": "oops"}') as User;
console.log(user.id.toFixed(2)); // compiles — crashes at runtime

// Because `JSON.parse` returns `any`, and `any` is assignable to anything.

That last block is the most important thing on this page. The types are a description of what you believe, and the compiler checks your code against that belief — not the world against it. Where data crosses into the program, somebody has to check it, and that somebody is you.

Superset, not a different language

Every valid JavaScript file is a valid TypeScript file. That is what makes adoption incremental: rename .js to .ts, fix what the compiler complains about, and add types where they help.

// Perfectly valid TypeScript — no annotations at all
const numbers = [1, 2, 3];
const doubled = numbers.map((n) => n * 2);
console.log(doubled); // [2, 4, 6]

// The types are inferred, and already doing work:
// doubled.toUpperCase(); // Property 'toUpperCase' does not exist on type 'number[]'

Inference means a well-typed codebase has far fewer annotations than people expect. You annotate the boundaries — function parameters, exported shapes, anything crossing a module — and let the compiler work out the middle.

Why it is worth the cost

Four claims, in rough order of how much they matter in practice:

  • The refactor you were avoiding becomes routine. Renaming a property, changing a signature, splitting a type — the compiler lists every call site, so the change is mechanical rather than a search-and-hope.
  • The editor knows what things are. Autocomplete, go-to-definition and inline errors come from the same type information, which is why the experience changes more than the error count does.
  • Types document the contract. A signature that says (id: number) => Promise<User | null> answers three questions a comment would have had to.
  • Bugs move left. A mistake found while typing costs minutes; the same mistake found in production costs a deploy.

The cost is real too: a build step, annotations to maintain, and a type system with enough corners to fill the rest of this guide.

Running it

npm install --save-dev typescript
npx tsc --init          # creates tsconfig.json
npx tsc                 # compiles, following the tsconfig
npx tsc --noEmit        # checks the types and writes nothing
npx tsc --watch         # re-checks on every save

--noEmit is the one most projects actually run. Vite, esbuild and SWC strip the types far faster than tsc can, but they do not check them — so tsc --noEmit in CI is often the only thing standing between you and a type error reaching main.

Where it is used

Angular and NestJS are written in it and assume it. React, Vue and Svelte all ship first-class types. Node has had @types/node for a decade, and recent versions can run .ts files directly. Deno and Bun take TypeScript with no configuration at all.

That ubiquity is itself an argument: at this point the question in an interview is rarely whether you use TypeScript.

Common mistakes

Believing the types protect the runtime

Mistake. Annotating an API response and treating the result as verified. Why it fails. The annotation is a claim, and JSON.parse returns any, which is assignable to anything. Nothing checked it. What to do instead. Validate at the boundary — a type guard, or a schema library — and let the type be the result of the check rather than a substitute for it.

Annotating everything

Mistake. const name: string = "Ana" and a return type on every internal helper. Why it fails. The compiler already inferred it, so the annotation is one more thing to keep in step and no more safety. What to do instead. Annotate what crosses a boundary — parameters, exports, public shapes — and let inference handle the rest.

Treating tsc as the build

Mistake. Assuming that because the app builds, the types are fine. Why it fails. Most build tools transpile without checking. The app builds whether or not the types are sound. What to do instead. Run tsc --noEmit separately, and in CI.

Check yourself

One question from the bank on what you just read. Answering here does not affect your quiz history.

What happens to TypeScript types when the code runs?

Practise Type annotations →

Further reading

Everything above is meant to be enough on its own. These are for when you want the full specification of a detail.

Reaching the end of a lecture marks it as read.