TypeScript practice
Practice TypeScript online: 10 exercises, graded problems and a playground
Every run here goes through the real TypeScript compiler, loaded into your browser, before the resulting JavaScript executes — so a type error stops the program with the same message tsc would give, and the exercises can teach types honestly instead of pretending they are comments. Three of the exercises begin with a program that deliberately fails to compile.
- checked exercises
- 10
- graded problems
- 0
- lessons
- 9
- cheat sheet
- 1
TypeScript practice exercises
How the path works →In order, each one introducing a single new idea. Press Run to check your answer; hints open one at a time.
- Checked, Then Runconsole.log() puts text on the screen
- Luggage Tagconst gives a value a name you can use again
- Egg Boxesnumbers do arithmetic, and Math.floor and % split a count into whole groups and a remainder
- The Ticket Machinea type annotation such as : number says what a name must hold, and a value that does not match stops the program before it runs
- The Fern Checka boolean is true or false, and if/else runs one of two blocks depending on it
- Parcel Postagea function takes typed parameters and returns a typed result
- The League Tabletemplate literals build text with values inside it
- The Number 12 Busan array type such as string[] holds several values of one type, and for...of walks through them
- The Departure Boardan object type lists the properties an object must have, and the compiler checks every object against it
- The Bike Shop Receiptassembling a complete program out of parts you already know
TypeScript lessons behind the practice
All lessons →- TypeScript: Checked, Then RunWhat actually happens when TypeScript runs your code - a compiler checks the whole program against the rules of every value's type, and only a program with no errors ever reaches console.log.
- Basic Types: string, number, and booleanHow TypeScript's three core primitive types work, the difference between writing a type annotation and letting the compiler infer one, and what happens the moment a value stops matching its type.
- Arrays and TuplesHow to type a growable list of same-kind values with an array type, how to type a fixed-length, position-specific group of values with a tuple, and why the two are not interchangeable.
- Interfaces and Object TypesHow to name the shape of an object with interface, what structural typing means and why it lets unrelated interfaces stand in for each other, and how optional and readonly properties change what the compiler allows.
- Functions and TypesHow TypeScript requires a type on every function parameter, how return types are checked against what a function actually returns, and how optional and default parameters change a parameter's type.
- Union and Literal TypesHow to say a value can be one of several types with the | operator, how a literal type narrows a type down to one exact value, and how typeof and if let you use a union value safely.
- Enums and Const AssertionsHow enum names a fixed set of related values you can refer to by a dotted name, how as const locks a value down to its most specific literal type, and where the two overlap with literal unions.
- Generics BasicsWhy writing a function against any throws away type safety, how a generic type parameter lets one function work for every type while TypeScript still checks it, and how to write a simple generic constraint.
- Type Narrowing and GuardsHow TypeScript narrows a union type across an if, the in operator, and a discriminated union's shared property, and how to write your own type predicate function to teach the compiler a new check.
TypeScript cheat sheet
Everyday syntax on one page, for when you remember the idea but not the spelling.
Coding interview questions
Original questions with full model answers, for when the practice is going somewhere specific.
Questions people ask
- Does the type checking actually run, or is this just JavaScript with annotations?
- It runs. The TypeScript compiler itself executes in your browser tab on every Run, and a program with a type error is refused with the real compiler diagnostic before any of it executes.
- Which TypeScript version is used?
- The version shown on the playground page, loaded from a copy of the official compiler package kept on this site rather than from a CDN. It is updated deliberately, not silently.
- Should I learn JavaScript first?
- It helps but is not required: the TypeScript exercises introduce the same basics — printing, variables, loops, functions — and add a type each time one makes the code clearer. If you already know JavaScript, the first few exercises will be quick.
- What is on this page besides the exercises?
- The 10 exercises are the start. After them: 9 in-depth lessons, a cheat sheet for the syntax and a blank playground.
Practise another language
The same kind of practice hub exists for Python, JavaScript, Java, C++, C, C#, Go, PHP, Ruby, SQL, R and Bash & the Shell.