Command Palette

Search for a command to run...

Hectal
Phase 16Advanced17 of 18 in TypeScript

Advanced TypeScript

How the checker really decides, inference in depth, declaration merging, type-level programming, the deliberate soundness holes, and designing types for a library.

Everything before this phase taught you to use the type system. This phase opens it up. You'll see exactly how the checker decides that one type fits another, why some casts are allowed and others refused, how it picks a type when it has several candidates, and why a few holes are left open on purpose.

You'll also learn the tools of people who write types for others to use: declaration merging (adding to a type from another file), type-level programming with tuples and template literal types, and the rules for designing a library's types so the error messages are good and the declaration files build fast.

Every claim here is demonstrated by a live example that the real compiler checks. Where a soundness hole lets a program crash, the console shows the crash, so you can see what the type system promised and what actually happened.

0/6 · 0%
6 topics ~2 h 34 code blocks & diagrams
Start with the first topic
1
16.1

How the Checker Thinks

The checker answers one question over and over: can a value of type S go where type T is expected? Knowing the exact rules (assignability, freshness, comparability, widening, narrowing) lets you predict every error before you see it.

25 min 5 code practice

2
16.2

Inference in Depth

Inference is the checker working out types you did not write. It collects candidates from every argument, uses the surrounding code as a hint (contextual typing), can also read the expected return type, and widens or freezes literals depending on the context.

24 min 4 code practice

3
16.3

Declaration Merging and Advanced Augmentation

When two declarations share a name, TypeScript sometimes merges them into one. Interfaces add members, namespaces attach values to functions, classes and enums, and module augmentation lets you add to a library's types without touching its files.

24 min 6 code practice

4
16.4

Type-Level Programming

Types can compute. With conditional types, infer, tuples and template literal types you can write small programs that run inside the checker: reverse a tuple, split a string, even add numbers. Knowing how, and when to stop, is the skill.

26 min 5 code practice

5
16.5

Soundness Holes and How to Stay Safe

TypeScript is deliberately not a fully sound type system: a few rules let wrong programs through so that real JavaScript stays easy to type. Each hole here is shown with the runtime crash it allows, and with the habit or lint rule that closes it.

22 min 7 code practice

6
16.6

Designing Types for a Library

When other people call your code, your types are the user interface. Choose overloads or generics deliberately, make inference do the work, shape the error messages callers will read, retire old APIs with @deprecated, and keep declaration files fast with isolatedDeclarations.

24 min 7 code practice

Practise this phase

Hands-on exercises: write the code, see your page live, and press Check to have it tested automatically.