Intermediate~4h

TypeScript: Types & Inference

Read what the checker inferred

TypeScript inferred a type for every line you wrote, annotated or not. This is the other half of the skill: reading what it concluded, narrowing a union until the thing you want to do is legal, and saying why an assignment was accepted or refused — on questions generated fresh that never run out.

Modules
5
Drills
10
Build steps
4
Time
~4h

Walk out able to

Name the type at any line, narrow a union until the call you want is legal, and say why the checker refused an assignment — without guessing.

What you work on

01

What was inferred

The syntax from memory, and the type you never wrote.

02

Unions and narrowing

Two shapes in one value, and the guard that picks one.

03

Shapes, not names

Why it fits — and why a fresh literal still does not.

04

any, unknown, never

Untyped data in; a forgotten case out, as a build error.

The pitch

What you practise, and what you leave with

A parser that takes an untyped value in, answers with a discriminated union, and turns a forgotten case into a build error.

You will practise

  • Write the type syntax from memory
  • Name the type nobody wrote down
  • Narrow a union until the call is legal
  • Decide which way a shape fits

Afterwards you can

  • Read an inferred type without hovering
  • Name the value a guard lets through
  • Explain why an object literal was rejected
  • Make an impossible state unrepresentable

Modules

5 modules, 37 items

Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.

  1. 1

    What the compiler already knows

    ~1h

    Write the type syntax from memory, and name the one a binding got with no annotation.

    • LessonWhy read a type you did not write4 min
    • LessonSee where a literal widens5 min
    • LessonFreeze a value with as const5 min
    • LessonCheck it without widening it4 min
    • LessonFind where inference gives up5 min
    • Build stepWatch the editor infer it10 min
    • DrillWrite the type from memory12 min
    • DrillName the inferred type11 min
    • DrillRead the return you never wrote11 min
  2. 2

    Two shapes in one value

    ~40 min

    Model a value that is one of several shapes, and say what you may touch before checking.

    • LessonCount the states a type describes4 min
    • LessonSay what a union permits5 min
    • LessonMake a set of values into a type4 min
    • LessonTag the shapes so they come apart6 min
    • DrillWhat a union lets you touch10 min
    • DrillRead a switch on the tag11 min
  3. 3

    Narrow until it is legal

    ~45 min

    Take a union down to one member, and name the value a guard still lets through.

    • LessonAsk what is still possible here4 min
    • LessonPick the guard that fits5 min
    • LessonFollow a narrowing down the body5 min
    • LessonWrite a type guard of your own4 min
    • LessonFind the guard that leaks4 min
    • DrillName the type at this line11 min
    • DrillFind what the guard misses12 min
  4. 4

    Shapes, not names

    ~44 min

    Say why an assignment is accepted, and why a fresh object literal still is not.

    • LessonCompare two types that never met4 min
    • LessonState assignability as a direction5 min
    • LessonExplain the rejected literal5 min
    • LessonSee where the checker is unsound4 min
    • LessonRead four error codes as sentences4 min
    • DrillDecide which way it fits11 min
    • DrillSpot the line freshness refuses11 min
  5. 5

    any, unknown, and never

    ~55 min

    Take an untyped value in safely, and make a missing case a compile error.

    • LessonSee the hole an any leaves4 min
    • LessonChoose between any and unknown5 min
    • LessonTurn a missing case into an error5 min
    • LessonShape a boundary that cannot lie4 min
    • DrillPick the right escape hatch11 min
    • Build stepDesign the result union8 min
    • Build stepNarrow the unknown input8 min
    • Build stepBreak the witness on purpose10 min

The checker already knows. Learn to read it.

The first item is free. ~4h of focused work, at your own pace.

Intermediate~4h5 modules