Type Guard — TypeScript

Sigil gate keeper: 20 levels of type-level TypeScript: keyof, mapped, conditional, infer, recursive and template literal types, checked by real tsc.

What you learn in this game

TypeScript tells a literal type apart from a wider type. "buka" holds one value; string holds every string. const, as const and annotations keep the literal; without them, TypeScript widens it to the primitive type.

From this door on, the relief is read off the prasasti. A carving read off the prasasti changes when the prasasti changes; a copied carving does not. That is why the gate turns the prasasti over and compares once more.

keyof takes the keys of an object type as a union of string literals: keyof { a: 1; b: 2 } is "a" | "b". When the shape of the type changes, its keyof changes too.

A conditional type looks like T extends U ? X : Y: if T fits in U, the result is X; if not, Y. Almost every utility type is built from this form.

A mapped type looks like { [K in U]: … }: one property for each K in the union U. With keyof and indexed access, Pick, Partial and their friends can be rebuilt by hand.

A type alias may call itself, as long as there is a stopping point. A common pattern: T extends object ? <call again> : T. A mapped type { [K in keyof T]: … } applied to a primitive type already stops by itself.

What the levels ask

  1. Candi Lawang Kembar at the end of the rice fields has twenty doors. Nenek Marni, its keeper, remembers the rule: a door opens when the right post is carved as the twin of the relief on the left post. The first door's relief is a single word, "buka". Write a type Result that is exactly that word, not every string; Bang Jaya carves it.
  2. From the second door on, the relief is read off the prasasti, the inscribed stone in front of the gate. This prasasti records Arca, the data of a statue in the temple. This door's relief is the type of nama on the prasasti: take it from Arca, do not copy it. The gate checks your carving against both faces of the prasasti, and its back face is different.
  3. The third door's relief holds every key name of Arca as a union. Read those names off the prasasti with one operator, so your carving follows when the prasasti changes.
  4. The fourth door asks about three things on the prasasti Uji: is it a string? Write IsString<T> that answers "ya" for a string and "tidak" for anything else. The Result line already calls it for all three.
  5. The fifth door is read not from a type but from a value: the prasasti records const juruKunci, written with as const. Take the type of that value without writing its shape again.
  6. The sixth door's prasasti records Pasangan, a tuple. Its relief is the union of everything in that tuple. Read it off the tuple with one index, so your carving stays right even if the tuple grows longer.
  7. Into the middle court. The seventh door asks for two keys of Arca, id and nama, with their types. Carve it by hand as a mapped type, without the lib's Pick: this gate wants to see you make it yourself.
  8. The eighth door wants Arca without dipahat. Carve it by hand, without Omit or Pick: a mapped type that walks every key of Arca and drops one, so new keys on the prasasti come along.
  9. The ninth door: every key of Arca may be missing. Carve it by hand as a mapped type, without Partial.
  10. The tenth door reads Draf, whose keys are all optional. Its relief: every key required. Carve it by hand, without Required.
  11. The eleventh door reads the plan Candi, objects inside objects. Its relief: everything readonly, all the way in. Write DeepReadonly<T>; the Result line already calls it for Candi.
  12. The twelfth door is for keepers and carvers only. The prasasti Peran records every role; drop "tamu" from that union with a utility from the lib.

And more, 20 levels in all.