The Complete TypeScript Course
Nine hands-on lessons that take you from vanilla JS to type-safe code shipping in CI — every step animated so you can see exactly what to do. 🎬
🚀 Start the course
Install & Setup TypeScript
Get the compiler, generate tsconfig.json, and turn your first .ts file into plain JavaScript. 👇
Install the compiler
TypeScript ships as the tsc CLI. Install it as a dev dependency:
npm install typescript --save-devGenerate tsconfig.json
This file controls everything — strictness, output folder, module system:
npx tsc --initTurn on "strict": true — it's the single best setting for catching bugs early.
Compile your first file
Write hello.ts, then compile — the output is plain .js any browser can run:
npx tsc hello.ts // → hello.jsWhich command generates a starter tsconfig.json?
Basic Types
Annotate your variables and let the compiler be your safety net — watch it catch a bug before your code ever runs.
Annotate with :
The syntax is variable: type. Once typed, TypeScript guards that variable forever:
let name: string = 'Ada'
let age: number = 36
let ok: boolean = true
let ids: number[] = [1, 2, 3]any turns TypeScript off for that variable. Prefer unknown — it forces you to check before using.
Which is valid TypeScript syntax?
Typing Functions
Type the parameters, type the return — and the compiler checks every single call site.
Params & return types
function greet(name: string): string {
return `Hello, ${name}`
}A function has no return statement. Its return type is…
Interfaces & Object Types
Describe the shape of your data once — every object that claims it gets fully checked.
interface — the shape contract
Use ? for optional props and readonly to lock them after creation:
interface User {
readonly id: number
name: string
age?: number // optional
}interface vs type? For object shapes they're near-identical — type can also do unions, interface can be augmented.
Which keyword describes the shape of an object?
Unions & Literal Types
A variable that accepts 'idle', 'loading' or 'success' — and nothing else. Perfect for state machines.
The | operator
Unions allow one of several types. Literal types restrict to exact values:
let id: string | number = 42
type Status = 'idle' | 'loading' | 'success'
let status: Status = 'loading'Typo 'loaidng'? Compile error — instead of a silent bug in production. That's the power of literals.
What does let id: string | number mean?
Classes & Access Modifiers
TypeScript classes add real access control — private members are blocked at compile time, not by convention.
public · private · readonly
class Counter {
private count = 0
increment() { this.count++ }
get value() { return this.count }
}Which modifier hides a member from outside the class?
Generics — <T>
Write a function once, keep full type-safety for every type you call it with. This is the <T> you see everywhere.
Type variables
T is a placeholder the caller fills in — input and output stay linked:
function first<T>(items: T[]): T {
return items[0]
}
first(['a', 'b']) // T = string → 'a'
first([1, 2, 3]) // T = number → 1What are generics for?
Narrowing & Type Guards
Union types are only useful if you can safely tell them apart at runtime. That's narrowing.
typeof narrows the branch
Inside an if, TypeScript knows exactly which member of the union you have:
function len(x: string | number) {
if (typeof x === 'string') {
return x.length // x: string
}
return x.toString().length // x: number
}Other guards: instanceof, 'prop' in obj, and discriminated unions — a shared kind literal field you switch on.
Checking typeof x === 'string' to split a union is called…
Compile, Watch & CI
Recompile on every save, keep strict mode on, and block broken code in your pipeline.
Watch mode
Recompiles instantly on every save — errors appear the moment you type them:
npx tsc --watchType-check in CI
--noEmit checks types without writing files — perfect as a pipeline gate:
name: Type Check
on: [push]
jobs:
tsc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npx tsc --noEmitAlso try tsc --noEmit --watch locally with bundlers like Vite/esbuild, which handle the JS output.
Which command recompiles automatically on save?
Comments
Comments
Post a Comment