Learn TypeScript
TypeScript adds static types on top of JavaScript and compiles away to plain JavaScript. These quizzes focus on what the compiler actually checks, how unions and generics work, and where types stop helping.
Written against TypeScript 5.x with strict mode on.
What you will practise
- What is checked at compile time versus runtime
- Unions, narrowing and discriminated unions
- Generics and utility types
- Strict mode and undefined handling
TypeScript lesson
A short introduction to the ideas behind the quizzes, before you start practising.
TypeScript is JavaScript with a type checker added in front. The checker catches many mistakes while you write code, then the types are erased and plain JavaScript is what actually runs.
Key concepts
Types are checked at compile time only
The compiler stops you from assigning the wrong kind of value, but once the code is compiled the types no longer exist. Anything that arrives at runtime, such as a JSON response, is only as safe as the checks you write for it.
let age: number = 21;
// age = "21"; // error: string is not assignable to numberUnions and narrowing
A union type such as `string | number` allows several types. Inside a check like `typeof x === "string"`, TypeScript narrows the type, so you can use string-only methods safely.
function show(x: string | number) {
return typeof x === "string" ? x.toUpperCase() : x.toFixed(1);
}Interfaces and optional properties
An interface describes the shape of an object. A property marked with `?` may be missing, so with strict mode on you must handle `undefined` before using it.
interface User { name: string; nickname?: string }
const label = (u: User) => u.nickname ?? u.name;Common mistakes
- Using `any` to silence an error, which switches off checking for everything that touches that value.
- Using `as Type` on data from outside the program and assuming it has been validated.
- Turning off strict mode to make errors go away instead of handling `undefined` and `null` properly.
- Expecting TypeScript types to exist at runtime, for example trying to check `if (x is User)`.
Quick recap
- Types are erased after compilation; validate external data at runtime.
- Prefer `unknown` over `any`, and narrow before use.
- Unions plus narrowing let one value have several possible types safely.
- Optional properties can be undefined; handle that case explicitly.
Ready to test this? Take the TypeScript Basics quiz →
Roadmap
Work through the levels in order, or jump to where you are.
Intermediate
The type system in depth
- Generics
- Utility types
- Discriminated unions
- never
TypeScript quizzes
- TypeScriptBeginner
TypeScript Basics
Static types, unions, interfaces and strictness
Compile-time vs runtimeType annotationsunknown vs any7 Qs~5mStart - TypeScriptIntermediate
Generics, Utility Types & Narrowing
Generics, discriminated unions, const assertions and runtime safety
GenericsUtility typesEnums vs unions7 Qs~5mStart
FAQ
Do I need to know JavaScript first?
Yes. TypeScript is JavaScript plus types, so comfort with JavaScript functions, objects and arrays helps a lot. The JavaScript quizzes are a good start.
Does TypeScript check types while the program runs?
No. Types are erased when the code is compiled, which is why data from outside your program still needs runtime validation.