🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
📘Free Course

TypeScript

JavaScript with superpowers — types, autocomplete, fewer bugs.

21 Lessonsintermediate

Lessons

1

Types for variables

In TypeScript you add types after a colon: let name: string = 'Alex'. The editor and compiler will warn you if you use the wrong type. Here we write valid JS that would be valid TS too (types in comments).

TypeScript was created by Microsoft (Anders Hejlsberg) and open-sourced in 2012Stack Overflow 2024 survey: TypeScript is the 5th most popular language overallTypes exist only at compile time — they're completely erased in the output JS
2

Types for functions

You can type function parameters and return value: function add(a: number, b: number): number { return a + b; }. That way you can't accidentally pass a string. Here we write the same in plain JS.

TypeScript infers return type from the return statement — annotation is optional but recommended for public APIsFunction types as variables: <code>type Handler = (event: Event) => void;</code>Overloads: multiple function signatures for the same function name
3

Interfaces (object shapes)

Interfaces define the shape of an object: which properties exist and their types. Use them for function parameters and return types. Here we write JS that matches an interface idea.

Interfaces are <strong>open for extension</strong> — you can re-declare the same interface in multiple files and TypeScript merges them (declaration merging)Interface vs type alias: both work for object shapes. <code>interface</code> is preferred for public APIs because it's extendableTypeScript uses <strong>structural typing</strong> — objects just need to match the shape, they don't need to explicitly implement the interface
4

Union types and narrowing

A union type is A | B: the value can be A or B. You then narrow (check) which one it is with typeof or checks, so TypeScript knows the type in each branch.

Discriminated unions with a <code>type</code> or <code>kind</code> field are the most powerful TypeScript patternTypeScript's control flow analysis tracks narrowing through if/switch/early return<code>never</code> type means 'this should never happen' — use in exhaustive switches
5

Array and tuple types

📚 What are Array and Tuple Types in TypeScript? TypeScript lets you be precise about what's inside an array. A typed array (number[]) guarantees every element is a number. A tuple [string, number] is like an array but with a FIXED length and FIXED types at each position -- perfect for pairs or tri…

Tuples are just arrays with additional compile-time length and type constraintsSpreading a tuple: <code>function add(...[a, b]: [number, number]): number { return a + b; }</code>ReadonlyArray prevents mutation — use for data you shouldn't change
6

Type assertions

📚 What are Type Assertions in TypeScript? A type assertion says to the TypeScript compiler: 'Trust me, I know what type this is.' You use the 'as' keyword: value as Type. This is NOT a runtime conversion -- TypeScript just stops complaining about that value. Use sparingly and only when you're cert…

Type assertions are erased at runtime — they produce no JavaScript outputNon-null assertion <code>!</code> is a shorthand for <code>as NonNullable&lt;T&gt;</code>The preferred safe alternative is a type guard: <code>if (input instanceof HTMLInputElement)</code>
7

Literal types

📚 What are Literal Types in TypeScript? A literal type is a type that is an exact specific value, not just any string or number. Instead of type string (any string), you can write 'red' | 'blue' | 'green' (only those three exact strings). TypeScript will then catch any typos or invalid values at c…

Template literal types: <code>type Greeting = `Hello, ${string}`;</code> — prefix/suffix patterns<code>as const</code> on arrays produces a readonly tuple of literal types — useful for enumsLiteral types are erased at runtime — they're purely compile-time
8

Optional and readonly

📚 What are Optional and Readonly Properties in TypeScript? Optional properties (name?: string) can be present or absent in an object. If the value is not provided, it will be undefined. Readonly properties (readonly id: number) can be set when the object is created but cannot be changed afterwards…

Optional properties exist only in TypeScript — JavaScript has no concept of required vs optional object keys<code>Required&lt;T&gt;</code> utility type removes all <code>?</code>; <code>Partial&lt;T&gt;</code> makes all properties optionalOptional chaining <code>?.</code> and nullish coalescing <code>??</code> are JavaScript features that pair naturally with TypeScript optional properties
9

Type aliases

📚 What are Type Aliases in TypeScript? A type alias gives a NAME to any type so you can reuse it. Instead of writing { name: string; age: number } everywhere, you write type User = { name: string; age: number } once and use User wherever you need it. Aliases work for objects, unions, functions, tu…

Type aliases and interfaces are mostly interchangeable for objects — the community convention: use <code>type</code> for unions/tuples/complex types; use <code>interface</code> for object shapes you might extendType aliases CANNOT be re-opened (unlike interfaces); you can't add properties later with declaration mergingUtility types like <code>Partial&lt;T&gt;</code>, <code>Required&lt;T&gt;</code>, <code>Pick&lt;T, K&gt;</code> are all type aliases
10

Generics basics

📚 What are Generics in TypeScript? Generics are 'type parameters' -- they let you write code that works with ANY type while still being type-safe. Instead of writing a separate function for numbers and a separate one for strings, you write one generic function and TypeScript figures out (or you te…

Generics are erased at runtime — they're purely a compile-time constructReact's own hooks use generics: <code>useState&lt;T&gt;</code>, <code>useRef&lt;T&gt;</code>The convention is single uppercase letters: <code>T</code> (type), <code>K</code> (key), <code>V</code> (value), <code>E</code> (element)
11

Enums

📚 What are Enums in TypeScript? An enum (enumeration) gives friendly names to a fixed set of values. Instead of checking if status === 'done' (where 'done' is a magic string that could be misspelled anywhere), you use Status.Done and TypeScript catches any typos. Enums make intent clear and preven…

Many TypeScript teams prefer union literal types over enums: <code>type Status = 'idle' | 'loading'</code> is simpler with less runtime overheadNumeric enums have a quirk: they allow reverse mapping AND any number is assignable to them — use string enums to avoid thisconst enum is erased entirely from JS output — pure compile-time
12

null and undefined

📚 null and undefined in TypeScript TypeScript distinguishes between null (explicitly no value) and undefined (value was never set). With strict mode enabled, TypeScript warns you whenever you try to use a value that might be null or undefined without checking first. Two modern operators help handl…

The billion dollar mistake: Tony Hoare (inventor of null) called null 'my billion-dollar mistake' due to the errors it causesstrictNullChecks is one of the most impactful TypeScript settings for catching real bugs<code>undefined</code> means 'not assigned'; <code>null</code> means 'intentionally empty' — use consistently
13

Return types and void

📚 What are Return Types and void in TypeScript? TypeScript lets you declare exactly what a function returns. This is useful for documentation and catching mistakes: if you say a function returns number but accidentally return a string, TypeScript catches it. void is the return type for functions t…

<code>never</code> is the bottom type — no value is ever of type nevervoid is assignable to undefined but not vice versa in strict modeThe exhaustive never pattern is the best way to ensure you handle all cases in a discriminated union switch
14

Index signatures

📚 What are Index Signatures in TypeScript? When you need an object that can have any number of string keys all mapping to the same value type -- like a dictionary -- you use an index signature: { [key: string]: number }. This says: 'this object can have any string as a key, and every value will be…

<code>Record&lt;K, V&gt;</code> is shorthand for an index signature: <code>{ [key in K]: V }</code>Index signatures allow all string keys — TypeScript can't narrow a specific key's typeFor known keys, use a proper interface or object type literal instead
15

Utility types: Partial, Pick

📚 What are Utility Types in TypeScript? TypeScript comes with built-in helper types called utility types that transform existing types. Instead of rewriting types from scratch for every variation, you can use Partial<T> (makes all optional), Pick<T, K> (keeps only specific keys), Omit<T, K> (remov…

All utility types are defined in TypeScript's lib.d.ts using mapped types and conditional types<code>ReturnType&lt;T&gt;</code> and <code>Parameters&lt;T&gt;</code> extract function signature types<code>Awaited&lt;T&gt;</code> unwraps a Promise type: <code>Awaited&lt;Promise&lt;User&gt;&gt;</code> → <code>User</code>
16

Narrowing with typeof and in

📚 What is Type Narrowing in TypeScript? Type narrowing is when TypeScript gets more specific about a type inside a conditional block. If a variable is string | number and you write if (typeof x === 'string') { ... }, TypeScript KNOWS inside that block x is a string -- so it unlocks string methods.…

TypeScript's narrowing is flow-based — it follows every branch and knows the type at each pointEarly return is the cleanest narrowing pattern: <code>if (!user) return; user.name; // safe</code>User-defined type guards: <code>x is User</code> return type lets you teach TypeScript about custom runtime checks
17

never type

📚 What is the never Type in TypeScript? The never type represents a value that can NEVER exist -- code that is unreachable. A function that always throws an error never returns a value, so its return type is never. The most powerful use of never is in exhaustive type checking: if you handle every …

never is the TypeScript equivalent of the mathematical empty set — ∅Conditional types use never as the 'not matching' branch: <code>T extends U ? X : never</code>Mapped types use never to exclude properties: filters where result is never get removed
18

Strict mode

TypeScript strict mode (strict: true) enables stricter checks: no implicit any, strict null checks, etc. It catches more bugs. Enable it for new projects.

19

TypeScript with React

React components can be typed: React.FC<Props> or function Comp(props: Props). Children: React.ReactNode. Events: React.ChangeEvent<HTMLInputElement>. State: useState<Type>(init).

20

tsconfig.json basics

tsconfig.json configures the compiler: target (ES version), module, strict, include/exclude. Set strict: true and noImplicitAny: true to catch more bugs.

21

TypeScript recap

You've seen: variable types, function types, interfaces, unions, narrowing, arrays/tuples, literals, optional/readonly, type aliases, generics, enums, null/undefined, utility types, never, strict mode, React types. TypeScript helps catch bugs and document code.

Start learning TypeScript free today

AI tutoring · quizzes · projects · works on any device