Question presented to candidate: "What are recursive types in TypeScript? Give me a data structure you would model with one, and then a type-level computation that uses recursion. What limits do you run into?"
What a strong answer should cover:
- A recursive type refers to itself, to describe nesting of unknown depth:
Json, trees, linked lists, expression ASTs, including mutual recursion between two types - The self-reference must go through a structure (object, array, tuple, generic); a bare
type Loop = Loop | stringis error TS2456. Type aliases became much more flexible here in TypeScript 3.7 - Recursive functions walk recursive types, and a discriminated union makes every case checked
- Recursive conditional types (TypeScript 4.1) compute types: deep array elements, all dotted paths of an object, tuple-length arithmetic
- The depth limit (error TS2589) and tail-recursive conditional types (TypeScript 4.5), which go about a thousand levels instead of a few dozen; the accumulator pattern
Code / implementation expected: Yes β a Json or tree type with a function over it, and one recursive conditional type.