TypeScript Introduction

TypeScript is a free, open-source programming language developed and maintained by Microsoft. It is a superset of JavaScript — every valid JavaScript program is also a valid TypeScript program — but TypeScript adds static type checking on top.

Think of it this way: JavaScript lets you write any code and hopes for the best at runtime. TypeScript catches your mistakes before the code even runs, in your editor or at compile time.

  • Created by Anders Hejlsberg (also the creator of C# and Delphi).
  • First released in 2012; now the most popular language for large frontend and backend Node.js codebases.
  • Compiles to plain JavaScript — browsers never see .ts files.
  • Used by Angular, Vue 3, SvelteKit, Next.js, Deno, and thousands of large companies.

Why TypeScript?

Here is a bug that JavaScript will not catch:

JavaScript
function add(a, b) {
  return a + b;
}
add("5", 3); // "53" — string concatenation, not addition

The function silently produces the wrong result. With TypeScript:

TypeScript
function add(a: number, b: number): number {
  return a + b;
}
add("5", 3);
// Error: Argument of type 'string' is not assignable to parameter of type 'number'.

The compiler catches the bug immediately — no runtime crash, no silent wrong answer.

What You Will Learn

This course covers the core TypeScript features you need for real-world projects:

Chapter Topic What you will know
Basic Types Primitives, arrays, tuples, enums Every built-in type
Interfaces Object shapes, type aliases How to describe data
Functions Parameters, overloads, callbacks Type-safe functions
Classes OOP, access modifiers, abstract Object-oriented TypeScript
Generics Type parameters, constraints Write reusable code
Modules import/export, namespaces Organise large projects
Unions & Intersections Composing types, narrowing Flexible type-safe APIs

Setting Up

You need Node.js (v18+) installed. Then:

Bash
# Install the TypeScript compiler globally
npm install -g typescript

# Check it works
tsc --version

# Create a new project
mkdir my-app && cd my-app
tsc --init          # generates tsconfig.json

The tsconfig.json file configures the compiler. Here is a sensible starting point:

JSON
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true,
    "outDir": "./dist",
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}
Option What it does
strict: true Enables all strict type-checking options — always turn this on
target: "ES2020" Output JavaScript version
outDir: "./dist" Where compiled .js files go
esModuleInterop: true Lets you import React from 'react' instead of import * as React

Compile and Run

Bash
# Compile once
tsc

# Watch mode — recompiles on every save
tsc --watch

# Run the output
node dist/main.js

Try It Yourself

TypeScript's official Playground lets you experiment with zero setup: typescriptlang.org/play.

Type any JavaScript, then hover over variables to see their inferred types. Add a type error and watch the red squiggly appear.

Key Terminology

Term Meaning
Type annotation You telling TypeScript the type: let x: number
Type inference TypeScript figuring out the type on its own: let x = 42
Superset TypeScript includes all of JavaScript plus its own additions
Compile Converting .ts to .js — TypeScript is not a runtime language
Strict mode strict: true in tsconfig — enables the strongest checking

What You Need

  • A text editor (VS Code with the TypeScript extension is ideal)
  • Node.js v18 or later
  • A terminal

Next: the type system — TypeScript's core superpower.

Knowledge Check

5 questions — test your understanding

1

What is TypeScript?

2

Which command compiles TypeScript files?

3

What does the : syntax do in TypeScript?

4

Which file configures the TypeScript compiler?

5

What does strict: true enable?