paulserban.eu

Writing Edition

Paul Serban

Writing, snippets & book notes

What is TypeScript? An In-depth Introduction to TypeScript for Beginners

Get Started with TypeScript: Your Comprehensive Guide to Understanding TypeScript's Features, Advantages, and Real-world Applications

Introduction

Embarking on the journey of web development comes with its own set of questions. One crucial question you'll likely encounter is what programming languages or technologies you should focus on. In the sea of languages like HTML, CSS, and JavaScript, another term you might stumble upon is TypeScript. So, what is TypeScript? In this comprehensive guide, we aim to answer that question in detail, helping you understand its fundamentals, its unique features, and why you might want to use it in your next project.

TypeScript has been steadily climbing the ladder of popularity since its inception. Developed and maintained by Microsoft, it is often seen as JavaScript's more disciplined sibling. But what sets TypeScript apart? How does it integrate with existing JavaScript frameworks and libraries? We will explore all these aspects, offering you a well-rounded understanding of this increasingly popular programming language. Whether you're a seasoned developer looking to broaden your skill set or a beginner wanting to make informed choices, this blog post is designed to serve as your comprehensive guide to TypeScript.

What is TypeScript?

The Basics

At its core, TypeScript is a superset of JavaScript, meaning that all valid JavaScript code is also valid TypeScript code. TypeScript essentially builds upon the capabilities of JavaScript, offering additional features and tools that aren't available in the latter. The most notable of these is static typing, which allows developers to specify the type of variables, function parameters, and return values. This feature brings several advantages, including better code quality and enhanced development tools, like autocompletion and error checking during compile time. Here is a basic TypeScript code snippet to demonstrate variable typing:

let myName: string = "John";
let myAge: number = 30;

Benefits Over JavaScript

So why bother with TypeScript when JavaScript is already so prevalent? The primary benefit is that TypeScript catches errors at compile-time rather than runtime. In simpler terms, TypeScript tells you when you've made a mistake before your code is run, saving you from hours of debugging. Moreover, TypeScript's type system makes the code more readable and self-documenting. With typed variables and functions, anyone reading the code can immediately understand the expected data types, making collaboration more efficient. TypeScript is highly beneficial in larger projects where the robustness and maintainability of code are crucial.

How TypeScript Differs from JavaScript

type Point = { x: number; y: number };
type Pixel = { x: number; y: number; color: string };

const p: Point = { x: 0, y: 0 };
const px: Pixel = { x: 0, y: 0, color: "#fff" };

// Structural typing allows this assignment (Pixel has at least x and y):
const p2: Point = px; // OK

Core Language Features (With Examples)

1) Type Inference

TypeScript often understands your types without annotations.

const count = 10;          // inferred as number
const names = ["Ana"];     // inferred as string[]

2) Unions and Narrowing

Model variability with precision and let the compiler narrow types based on control flow.

type Result = { ok: true; data: string } | { ok: false; error: Error };

function handle(r: Result) {
  if (r.ok) {
    console.log(r.data.toUpperCase()); // r is narrowed to { ok: true; data: string }
  } else {
    console.error(r.error.message);    // r is narrowed to { ok: false; error: Error }
  }
}

3) Interfaces vs Type Aliases

Both describe shapes; interfaces are best for object shapes and declaration merging; type aliases excel at unions and advanced composition.

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

type ApiResponse<T> = { ok: true; data: T } | { ok: false; error: string };

4) Generics

Create reusable, type-safe utilities.

interface Repository<T, Id = string> {
  get(id: Id): Promise<T | null>;
  save(entity: T): Promise<void>;
}

5) Literal Types and Enums (Prefer Unions)

Prefer string literal unions for lighter bundles and better tree-shaking.

type Role = "admin" | "user";

function canEdit(role: Role) {
  return role === "admin";
}

6) unknown vs any

function parse(json: string): unknown {
  return JSON.parse(json);
}

const value = parse('{"x":1}');
if (typeof value === "object" && value !== null && "x" in value) {
  // safe to use now
}

7) Utility Types and Template Literal Types

type ReadonlyUser = Readonly<{ id: string; name: string }>;
type Endpoint<T extends string> = `/api/${T}`;
const usersEndpoint: Endpoint<"users"> = "/api/users";

Getting Started: Tooling and Setup

Install and Initialize

A beginner-friendly tsconfig:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true,
    "moduleResolution": "Bundler",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "dist"
  },
  "include": ["src"]
}

Run and Build

Linting and Formatting

TypeScript with Popular Frameworks

React + TypeScript

type GreetingProps = { name: string; isAdmin?: boolean };

export function Greeting({ name, isAdmin = false }: GreetingProps) {
  return <h1>Hello {isAdmin ? "Admin " : ""}{name}</h1>;
}

Node/Express + TypeScript

import express from "express";
const app = express();
app.get("/health", (_req, res) => res.json({ ok: true }));
app.listen(3000);

Angular

Angular is built with TypeScript. Using Angular CLI (ng new) sets up TypeScript automatically with strong typings throughout components and services.

Working with Third-Party Types

Gradual Migration from JavaScript

You can adopt TypeScript incrementally.

  1. Add JSDoc types in .js files:
/**
 * @param {number} a
 * @param {number} b
 */
export function add(a, b) {
  return a + b;
}
  1. Turn on type checking for JS:
{ "compilerOptions": { "checkJs": true, "allowJs": true }, "include": ["src"] }
  1. Convert files one by one to .ts/.tsx.
  2. Enable "strict": true early; suppress intentionally and track cleanups.

Best Practices for Beginners

Common Pitfalls (And How to Avoid Them)

Real-World Patterns You'll Use

Discriminated Unions for State Machines

type AuthState =
  | { status: "logged_out" }
  | { status: "loading" }
  | { status: "logged_in"; user: { id: string; name: string } }
  | { status: "error"; message: string };

Branded (Nominal-like) Types to Avoid Mix-ups

type Brand<T, B extends string> = T & { __brand: B };
type UserId = Brand<string, "UserId">;
type OrgId = Brand<string, "OrgId">;

function getUser(id: UserId) {}
// getUser("abc") // Error if "abc" isn't branded as UserId

API Layer Types

Define request/response DTOs in one place and reuse across client and server for end-to-end typing.

Where TypeScript Might Not Be Necessary

Even then, JSDoc with checkJs can provide a lightweight safety net.

Quick Start Checklist

FAQ

Conclusion

TypeScript is not just another programming language; it's an invaluable tool for modern web development. By offering features like static typing, it addresses some of the limitations of JavaScript, making your code more robust, readable, and maintainable. Whether you are working on a small project or a large enterprise application, TypeScript provides tools and features that can significantly improve your development workflow.

In this post, we've covered the basics of TypeScript, how it compares to JavaScript, and its real-world applications. We also walked through setup, best practices, and patterns that scale. As you embark on your web development journey, consider adding TypeScript to your toolkit. Its compatibility with popular frameworks and libraries, combined with its own set of features, makes it a compelling choice for developers aiming for efficiency, scalability, and maintainability in their projects. If you're looking to step up your coding game, TypeScript is certainly worth your attention.

Happy typing!