Course topics

By WebNest Studio Engineering Team

JavaScript Tutorial

Introduction to TypeScript

TypeScript is a typed superset of JavaScript developed by Microsoft: every valid JavaScript program is also valid TypeScript, but TypeScript adds an optional static type system on top. TypeScript code is never run directly by a browser or Node.js — it is compiled ("transpiled") down to plain JavaScript by the TypeScript compiler (tsc) or a build tool, and it is that resulting JavaScript that actually executes.

The core value proposition is catching a whole category of bugs at compile time — before the code ever runs — that plain JavaScript would only reveal at runtime, often in production, in front of a real user. This single benefit is why TypeScript has become the de facto standard for professional JavaScript codebases, from large frontend applications to backend Node.js services.

Why it matters

TypeScript adds types to JavaScript and checks them before the code runs, which catches misspelled properties, missing arguments and undefined values while you type. Most new front-end and Node.js projects use it, and most job listings for JavaScript roles now ask for it.

Basic Type Annotations

You annotate a variable, parameter, or return value with a colon followed by a type: let age: number = 25; or function greet(name: string): string { ... }. If you later try to assign a value of the wrong type, TypeScript reports an error immediately in your editor, long before you run the code.

  • Primitive types: string, number, boolean.
  • Array types: number[] or Array<number>.
  • any opts out of type checking entirely for that value — useful sparingly, but overusing it defeats the purpose of TypeScript.
  • unknown is a safer alternative to any: it forces you to narrow the type before using the value.

Interfaces

An interface describes the expected shape of an object — which properties it must have and what types they are. Interfaces are a purely compile-time construct: they exist to help the type checker (and other developers reading the code) and produce no JavaScript output at all once compiled.

Compile-Time Errors vs. Runtime Errors

In plain JavaScript, passing the wrong shape of object to a function often does not fail immediately — it may silently produce undefined, or throw an exception minutes later deep inside unrelated code, far from the actual mistake. TypeScript's compiler flags the mismatch at the moment you write it, pointing directly at the offending line, which is a dramatically faster and cheaper feedback loop than discovering the same bug in production.

Example

An interface and typed function catching a mistake before the code ever runs

JavaScript
interface User {
  id: number;
  name: string;
  email: string;
}

function sendWelcomeEmail(user: User): string {
  return `Welcome, ${user.name}! A confirmation was sent to ${user.email}.`;
}

const validUser: User = { id: 1, name: 'Asha', email: 'asha@example.com' };
console.log(sendWelcomeEmail(validUser));

// The next line is a compile-time error, caught before running:
// sendWelcomeEmail({ id: 2, name: 'Ravi' });
// Error: Property 'email' is missing in type '{ id: number; name: string; }'
// but required in type 'User'.
Output
Welcome, Asha! A confirmation was sent to asha@example.com.
(The commented-out call fails to compile with: Property 'email' is missing in type '{ id: number; name: string; }' but required in type 'User'.)

Common Mistakes

  • Overusing the "any" type to silence errors quickly, which quietly disables type checking for that value and reintroduces the exact runtime bugs TypeScript exists to prevent.
  • Forgetting that TypeScript must be compiled to JavaScript before it runs anywhere — browsers and Node.js cannot execute .ts files directly.
  • Assuming TypeScript checks values at runtime — its type system is erased entirely during compilation and provides zero runtime protection against, say, unexpected API responses unless you validate them separately.
  • Writing overly complex, deeply nested types for simple data early on, when a plain interface and a few basic types would communicate the same intent more clearly.

Exercise

This exercise is TypeScript, so it needs the TypeScript compiler or an online TypeScript playground; the JavaScript playground here cannot run it. Declare an interface Product with a name that is a string and a price that is a number. Write a function total that takes an array of products and returns a number. Then see what the compiler says about the commented-out call.

Expected result: the program prints 162, and uncommenting the last line gives a compile-time error because price is a string.

JavaScript · starter code
// TODO: interface Product

// TODO: function total(items) with type annotations for the parameter and the return value

console.log(total([{ name: "pen", price: 12 }, { name: "book", price: 150 }]));

// console.log(total([{ name: "bag", price: "450" }]));
Show hints
  • An interface lists property names and types: interface Product { name: string; price: number; }.
  • An array of products is written Product[], and the return type follows the parameter list after a colon.
Show solution
JavaScript · solution
interface Product {
  name: string;
  price: number;
}

function total(items: Product[]): number {
  return items.reduce((sum, item) => sum + item.price, 0);
}

console.log(total([{ name: "pen", price: 12 }, { name: "book", price: 150 }]));

// console.log(total([{ name: "bag", price: "450" }]));

Quick quiz

1. What happens to TypeScript code before a browser runs it?

  • A. The browser runs it directly
  • B. It is compiled to JavaScript, with the types removed
  • C. It is converted to WebAssembly
  • D. It is interpreted by Node.js only
Show answer

B. Types exist only while the code is being written and compiled.

2. When are TypeScript type errors reported?

  • A. Only in production
  • B. Only when the faulty line runs
  • C. At compile time, before the code runs
  • D. Never; types are comments
Show answer

C. The editor also shows them as you type.

3. How is a function parameter given a type?

  • A. function greet(string name)
  • B. function greet(name as string)
  • C. function greet(name = string)
  • D. function greet(name: string)
Show answer

D. The type follows the name after a colon.

Interview questions

What are the benefits of TypeScript over JavaScript?

Show answer

It finds type errors before the code runs: wrong argument types, misspelled property names, values that may be undefined. Types serve as documentation that cannot go out of date, and they give editors accurate autocompletion and safe refactoring. In large codebases and teams this greatly reduces bugs. The costs are a build step and some extra code to write.

What is the difference between an interface and a type alias?

Show answer

Both can describe the shape of an object. An interface can be extended with extends and can be declared more than once, with the declarations merged. A type alias can name any type, including unions, intersections, tuples and primitives, which an interface cannot. A common convention is interfaces for object shapes and type aliases for unions and everything else.

Key Points to Remember

  • TypeScript is a typed superset of JavaScript: valid JS is valid TS, and TS compiles down to plain JS to actually run.
  • Type annotations (string, number, boolean, arrays, custom interfaces) let the compiler check correctness before code ever executes.
  • Interfaces describe object shapes at compile time and produce no runtime JavaScript themselves.
  • Catching type mismatches at compile time rather than at runtime is TypeScript's core value, which is why it has become standard in professional JS codebases.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering