Back to Interview Prep
Course progress0/12 chapters · 0%

Chapter 1 of 13

~10 min

Introduction to TypeScript

TypeScript is a typed superset of JavaScript that compiles down to plain JavaScript, adding a static type system on top of a language that otherwise stays exactly the same at runtime.

What "Superset of JavaScript" Really Means

Every valid JavaScript file is already valid TypeScript - TypeScript does not replace the language, it layers optional type annotations, interfaces, and compile-time checks on top of it. This means an existing JavaScript codebase can usually be renamed from `.js` to `.ts` and still compile, even before a single type annotation has been added.

The types themselves have no representation at runtime. They exist purely to help the compiler (and your editor) catch mistakes before the code ever runs, and they are stripped away entirely during compilation - the JavaScript that ships to a browser or Node.js has no trace that TypeScript was ever involved.

Why Static Typing Helps

In plain JavaScript, a typo like calling `user.emial` instead of `user.email` is only discovered when that line actually executes - possibly in production, possibly rarely, depending on which code path triggers it. TypeScript's compiler checks every reachable line against declared types, so many classes of bugs (wrong property names, passing a string where a number is expected, forgetting a required function argument) surface immediately, at compile time, instead of as a runtime crash.

Static types also serve as always-up-to-date documentation. A function signature like `function total(items: CartItem[]): number` tells you exactly what to pass in and what you get back, without needing to read the implementation or trust a comment that might have gone stale.

Compiling with tsc

The TypeScript compiler, `tsc`, reads `.ts` files, type-checks them, and emits plain `.js` files (plus optional `.d.ts` declaration files and source maps). Running `tsc` with no arguments looks for a `tsconfig.json` in the current directory and compiles the project it describes; running `tsc --init` generates a starter `tsconfig.json` for you.

During everyday development, most projects run `tsc` in watch mode (`tsc --watch`) or rely on a bundler (Vite, webpack, esbuild) that calls into the TypeScript compiler API itself, so you rarely invoke `tsc` by hand outside of a final production type-check.

A first TypeScript file and compiling it

// greet.ts
function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet('Ada'));
// console.log(greet(42)); // compile-time error: number is not assignable to string

// Compile with:
//   tsc greet.ts
// This produces greet.js, plain JavaScript with the types stripped out.

Basic Type Annotations

Annotations follow a variable, parameter, or return type with a colon and the type, such as `let age: number = 30`. You do not need to annotate everything - TypeScript infers types from initial values wherever it safely can, and annotations are most valuable on function boundaries (parameters and return types), since that is where the compiler has nothing else to infer from.

Coding Challenge

Write a small TypeScript file with a function `square(n: number): number` that returns the square of a number, and call it with a valid number to log the result. Include a comment showing what error `tsc` would report if you tried calling it with a string.

Answer every question below to mark this chapter as complete.

Quiz Yourself

Score: 0/5

1. What is TypeScript, most precisely?

2. What happens to TypeScript type annotations when the code is compiled?

3. Why does static typing help catch a bug like `user.emial` (a typo for `user.email`) earlier?

4. What does running `tsc` with no arguments do in a project that has a tsconfig.json?

5. When are TypeScript annotations most valuable to add explicitly, given that inference exists?

This is a free preview lesson. Sign up to unlock the full course.Sign up freeSign in