Description
This guide covers the fundamentals of TypeScript: the building blocks you will use in every project from here on. It replaces the four separate introduction guides — data types, conditionals, loops and functions — because they are one subject, and because you learn them by writing code rather than by writing four separate hand-ins.
JavaScript is the only programming language every browser understands, which makes it a building block of web development. With it you add behaviour: interaction, data collection, changing the interface based on what the user does, and requesting data from other services. A lot of the time it works alongside HTML and CSS, but it does not need them — the browser adds Web APIs that let JavaScript manipulate the page, and before you get there it is worth understanding what plain JavaScript can do on its own.
TypeScript is JavaScript with types. A browser cannot run TypeScript directly; it compiles down to JavaScript first. The types are for you and your editor — they catch a whole class of mistake before the code ever runs. You do not need a website to try any of this: write a TypeScript file, compile it, and read the output in your terminal or the browser console. Publishing to GitHub Pages is for when you want to show someone.
What this guide covers
- Data types — variables, numbers, strings, booleans, arrays and objects, and the types that describe them
- Conditionals — if/else, logical operators, the ternary operator, switch
- Loops & iterators — for, for…of, while, and the array methods that often replace them
- Functions — parameters, return values, scope, arrow functions and callbacks
How this guide is graded
Instead of a hand-in and a peer review, this guide has an exercise that grades itself. It mixes questions with real coding problems: you write TypeScript, and it is compiled and run against test cases in front of you.
- Unlimited attempts, and your best score is the one that counts. Getting it wrong costs you nothing.
- Your questions and coding problems are drawn from a larger pool, so no two people get exactly the same set. Yours stay the same while you work on them, so you can come back to a half-finished attempt.
- Your code is type-checked before it runs. Add types to your variables; the grader reads them.
- Some test cases are hidden. Your solution has to work in general, not just on the examples.
Going further: Codewars
The exercise is the assessment, but if you want to keep practising — and functions are the thing worth over-practising — go to Codewars and solve katas in TypeScript. Register, join the clan called Vefskólinn, and start on an 8 kyu problem. When 8 kyu gets boring take a 7 kyu, and see if you can get far enough to solve a 6 kyu.
To make it competitive: anyone who earns more honour from katas than me before the Las Palmas trip gets dinner on me one of the evenings we are out there. Have a look at my Codewars profile to see what you need to beat.