jvinhit//lab

Search posts

Type to search across journal entries.

navigate open esc close

Series · 11 parts

TypeScript Type Challenges — Zero to Type Wizard

Master type-level programming by solving the famous type-challenges, one concept at a time. Start with the five building blocks and a guided tour, then go deep on mapped types, conditional types and distribution, infer, recursion over tuples, union manipulation, template-literal string math, type-level arithmetic, the hard utility types, parsers and state machines, and a few extreme challenges — finishing with a capstone that turns it all into a reusable typed library. Every challenge shows the goal first, hides the answer behind a toggle, then explains the solution step by step. Solutions follow the canonical community approaches from the MIT-licensed type-challenges project.

  1. Part 2: master the mapped type. Rebuild Partial, Required, Mutable, Readonly 2, PartialByKeys, Merge and ReplaceKeys; add and remove the readonly/? modifiers; and key-remap with as — each with a toggle-to-reveal answer.

  2. Part 3: the A extends B ? X : Y engine. Build If, IsNever, IsUnion and AnyOf, nail the distributive rule (and switch it off with [T]), and decode the Equal gadget behind every test — each with a toggle-to-reveal answer.

  3. Part 4: capture pieces of a type with infer. Rebuild Parameters, Last and Pop, append arguments to functions, and learn constrained infer (infer N extends number) to parse numbers from strings — each with a toggle-to-reveal answer.

  4. Part 5: peel one element off and loop. Build Push, Unshift, Reverse, Includes, Flatten and Join with recursion over tuples, learn the accumulator pattern, and understand the recursion depth limit — each with a toggle-to-reveal answer.

  5. Part 6: unions are not tuples. Turn a string into a union, recap distribution, extract the last member of a union, and build the famous UnionToTuple with the contravariance gadget — each with a toggle-to-reveal answer and explanation.

  6. Part 7: string math at the type level. Build Trim, Replace, ReplaceAll, StartsWith/EndsWith and LengthOfString with template-literal pattern matching and recursion — each with a toggle-to-reveal answer and a step-by-step explanation.

  7. Part 8: TypeScript has no + for numbers, so we count with tuple lengths. Build BuildTuple, Add, Subtract, GreaterThan and Fibonacci using the accumulator and tuple-length tricks — each with a toggle-to-reveal answer and explanation.

  8. Part 9: type utilities from real libraries. Split objects by required vs optional keys with GetRequired, RequiredKeys and OptionalKeys, then build a fluent, type-safe Chainable builder — each with a toggle-to-reveal answer.

  9. Part 10: parse strings at the type level. Convert camelCase to kebab-case, fold a tuple into a nested object, and build a real printf format parser that walks a string as a state machine — each with a toggle-to-reveal answer.

  10. Part 11: the hardest entries, taken slowly. Build Currying, a dotted-path Get, and IsPalindrome by composing patterns from the whole series — proof that extreme types are just familiar tricks stacked. With toggle-to-reveal answers.

  11. Part 12: turn the series into a real, fully-typed deep-get utility with autocompleted paths, then master the meta-skills — debugging types, recursion and performance limits, a one-page pattern cheat sheet, and a practice plan.