Why is it safe to return a subclass instead of a superclass, but dangerous to accept a superclass parameter instead of a subclass? Welcome to Variance: the computer science underpinning of TypeScript’s assignability engine.
An advanced masterclass in TypeScript type-level metaprogramming. Master conditional types (T extends U ? X : Y), distributive behavior, infer keyword pattern matching for unwrapping types, template literal types (${T}_${U}), and deep recursive object transformations.
Use mapped types and generics to enforce finite state transitions in the compiler. A type-level state machine makes impossible states impossible to represent, eliminating entire classes of runtime bugs.
How do you type a JSON object? A JSON object can contain arrays of objects, which can contain arrays of objects, infinitely down. You cannot hardcode this depth. You need a Recursive Type.
Template literal types allow you to use JavaScript-style template strings (${...}) inside Type Space. Learn how to generate combinations of string literals automatically via Union Permutation, and parse delimited strings (like emails or URLs) using the infer keyword.
Pattern matching with infer isn’t just for Promises and Objects. You can deconstruct Tuples and Arrays element by element in Type Space, mirroring the exact syntax of JavaScript’s array destructuring and spread operators.
If conditional types are the if-else statements of Type Space, the infer keyword is the variable declaration (let x = ...). Learn how to extract types dynamically via pattern matching, rebuild ReturnType, and parse Template Literal Strings at compile time.
Sometimes, you want a conditional type to evaluate an entire Union as a single monolithic entity, rather than mapping over its individual members. Learn how wrapping generic parameters in brackets [T] prevents distribution, and how to use this mechanic to build complex IsUnion checks.
When you pass a Union type into a conditional type, TypeScript doesn’t evaluate the union as a whole. It evaluates every single member of the union separately, and combines the results. Learn how this powerful (and sometimes confusing) mechanism powers TypeScript’s standard library.
Conditional types are the if-else statements of Type Space. Learn how to use the ’extends ? :’ syntax to compute dynamic types based on assignability logic, replace complex function overloads, and construct nested else-if type chains.