Background image
logologo symbol

3 - The fundamentals

TypeScript Introduction (8h-10h.)

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.

Topics

  • Fundamentals — comments, output (console.log), variable declaration (let and const), assignment
  • Data types — numbers and arithmetic, strings, concatenation and template literals, booleans, truthy and falsy
  • Arrays — zero-based indexes, length, sort()
  • Objects — syntax, dot notation, adding and removing properties, destructuring
  • TypeScript — annotating variables and parameters, return types, type aliases, compiling to JavaScript
  • Conditionals — if/else, logical operators (!, ||, &&), the ternary operator, switch/case, break
  • Loops & iterators — for, for…of, for…in, while, break, and array methods
  • Functions — declaring and calling, parameters, default parameters, return, scope, arrow functions, callbacks and higher-order functions

Goals

Knowledge

Understand what a programming language is, and what JavaScript is
Understand how JavaScript can work with and without the browser
Understand what data types are, and how TypeScript adds types on top of JavaScript
Understand what conditional statements are
Understand what loops and iterators are
Understand what functions, parameters and return values are

Skills

Be able to use variables, arrays and objects with the right types
Be able to execute conditional statements
Be able to use loops and iterators to work through a list of data
Be able to write functions that take parameters and return a value
Be able to read a TypeScript error and fix what it points at

Idea for return

There is no hand-in for this guide and no peer review. Work through the exercise below: it mixes questions about the concepts with coding problems where you write TypeScript and have it compiled, type-checked and run against test cases.
Take as many attempts as you like — your best score is what counts. Read the type errors when you get them; learning to read what the compiler is telling you is part of the point.
If you want more practice afterwards, the Codewars track described above is open all module — and the dinner bet stands.

Materials