Type-level challenge set
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Solve small type puzzles with conditional, mapped and template types.
- Prove each answer with a compile-time assertion.
- Explain the algorithm behind each type.
Overview
#How to practice
Each challenge asks for a type. Prove it with the Equal and Expect helpers from the capstone.
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type Expect<T extends true> = T;
Challenge 1: First
type First<T extends readonly unknown[]> = T extends readonly [infer H, ...unknown[]] ? H : never;
Challenge 2: Length
type Length<T extends readonly unknown[]> = T["length"];
Challenge 3: Concat
type Concat<A extends readonly unknown[], B extends readonly unknown[]> = [...A, ...B];
Challenge 4: Camel-case a key
type Camel<K extends string> = K extends `${infer H}_${infer T}`
? `${H}${Capitalize<Camel<T>>}`
: K;
Method
Read the input type, decide the pattern (tuple, union, string), pick the tool (infer, mapped, template), and assert the result. If the assertion fails, the checker tells you the actual type.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Implement `First<T extends readonly unknown[]>` that returns the first element or never for an empty tuple.
starter.typescriptexport type First<T extends readonly unknown[]> = unknown;Reveal the reference answer
reference.typescriptexport type First<T extends readonly unknown[]> = T extends readonly [infer H, ...unknown[]] ? H : never;Authored expected output: Compile-time: First<[1, 2]> is 1; First<[]> is never. No runtime output.
The tuple pattern with a rest element matches only non-empty tuples.
Exercise 2Implement
Implement `Concat<A, B>` that concatenates two tuple types.
starter.typescriptexport type Concat<A extends readonly unknown[], B extends readonly unknown[]> = [...A];Reveal the reference answer
reference.typescriptexport type Concat<A extends readonly unknown[], B extends readonly unknown[]> = [...A, ...B];Authored expected output: Compile-time: Concat<[1], [2, 3]> is [1, 2, 3]. No runtime output.
Spread elements in a tuple type concatenate the two tuples.
Exercise 3Test
Write assertions proving `First` and `Concat` using `Equal` and `Expect`.
starter.typescriptexport type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; export type Expect<T extends true> = T; export type First<T extends readonly unknown[]> = T extends readonly [infer H, ...unknown[]] ? H : never; export type Concat<A extends readonly unknown[], B extends readonly unknown[]> = [...A, ...B]; // add assertions hereReveal the reference answer
reference.typescriptexport type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; export type Expect<T extends true> = T; export type First<T extends readonly unknown[]> = T extends readonly [infer H, ...unknown[]] ? H : never; export type Concat<A extends readonly unknown[], B extends readonly unknown[]> = [...A, ...B]; export type FirstCheck = Expect<Equal<First<[1, 2]>, 1>>; export type EmptyCheck = Expect<Equal<First<[]>, never>>; export type ConcatCheck = Expect<Equal<Concat<[1], [2, 3]>, [1, 2, 3]>>;Authored expected output: Compile-time only: the file compiles when all three assertions hold. No runtime output.
The assertions turn type behavior into a compile-time test suite.
Common misconceptions
#- Type challenges need runtime code.
- There is only one correct solution.
- The Equal helper checks values.
Follow-up practice
#- typescript-error-handling
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.