courses / typescript / level 2

level 2 · medium

intermediate
typescript.

Build useful typed programs: shape real data, handle missing values and talk to a server safely.

  • 28 lessons
  • 7 units
  • 4 h 19 m in all
  • £0, for good

what’s inside.

For people who finished Level 1, or can already write small programs with variables, decisions, loops and functions.

TypeScript Level 2 picks up where Level 1 ended and turns your typed basics into small, useful programs. In 28 lessons you shape data with interfaces, unions and generics, build classes, and fetch replies from Gwecode's practice server, checking each one before you trust it. It ends with a level check: a weekend weather planner that copes with whatever the server sends.

every lesson goes the same way.

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

unit 1

Shapes of data

Describe your data so the checker knows what every value holds.

4 lessons · 35 min

  1. Objects8 minKeep related facts together and reach each one with a dot.
  2. Interfaces9 minName the shape of your objects once and use it everywhere.
  3. Type aliases8 minGive any type a name that says what it is for.
  4. Objects working together10 minPass objects to functions, line them up in arrays and nest them.

unit 2

This or that

Let a value be one of several things, and find out which.

4 lessons · 34 min

  1. Union types8 minLet a box hold one type or another, and nothing else.
  2. Literal types8 minAllow only the exact values that make sense.
  3. Narrowing9 minCheck which type you have, so the checker lets you use it.
  4. Choosing with switch9 minJump straight to the right case instead of a long chain of checks.

unit 3

Maybe, and never

Handle values that might be missing, and lock values that must stay.

4 lessons · 33 min

  1. Optional properties8 minDescribe objects where some facts may be missing.
  2. Reading safely8 minRead properties that might be missing, in one short line.
  3. Optional and default parameters9 minLet callers leave out arguments, with sensible fallbacks.
  4. readonly8 minLock the facts that must never change.

unit 4

Lists, pairs and lookups

Work through arrays quickly, pair values up and look things up by name.

4 lessons · 35 min

  1. Arrow functions8 minWrite small functions in one short line.
  2. map and filter10 minChange every item, or keep only some, without writing a loop.
  3. Tuples8 minKeep a few values together in a fixed order, each with its own type.
  4. Record9 minBuild lookup tables that match keys to values.

unit 5

Build your own types

Write code that works for any type, and build objects from classes.

4 lessons · 40 min

  1. Generic functions10 minWrite one function that works for every type and keeps track of which.
  2. Generic types9 minMake interfaces that wrap any type you choose.
  3. Classes11 minBuild many objects from one blueprint, each with its own methods.
  4. Private and implements10 minGuard a class's data, and make it keep an interface's promise.

unit 6

Waiting for answers

Write code that waits for timers and for replies from a server.

4 lessons · 40 min

  1. Timers9 minRun code later, while the rest of your program carries on.
  2. Promises11 minWait for a value to arrive, one step after another.
  3. Asking the practice server10 minSend a request to a server and read its reply.
  4. Typing replies10 minTurn JSON from a server into typed objects.

unit 7

Trust, but check

Check data as it arrives, and recover when things go wrong.

4 lessons · 42 min

  1. unknown9 minHold any value safely until you have checked what it is.
  2. Errors on purpose10 minRaise your own errors, and catch them before they crash the program.
  3. Checking data at runtime11 minTurn unknown data into a typed object, or refuse it.
  4. Putting it all together12 minFetch, check and report, and cope with whatever comes back.
end of level 2 level check: a weekend weather planner Build one small project with everything from this level. Pass it and your face levels up. see the check

next, level 3

advanced
typescript.

Write TypeScript like a professional: types that build types, unions you can trust, and tested, shipped tools.

see level 3