Skip to article content
FrontendXLesson 35 / 40

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.

advanced · about 55 minutes · 3 exercises

  • Implement
  • Test

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.

snippet.ts
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

snippet.ts
type First<T extends readonly unknown[]> = T extends readonly [infer H, ...unknown[]] ? H : never;

Challenge 2: Length

snippet.ts
type Length<T extends readonly unknown[]> = T["length"];

Challenge 3: Concat

snippet.ts
type Concat<A extends readonly unknown[], B extends readonly unknown[]> = [...A, ...B];

Challenge 4: Camel-case a key

snippet.ts
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.

  1. Exercise 1Implement

    Implement `First<T extends readonly unknown[]>` that returns the first element or never for an empty tuple.

    starter.typescript
    export type First<T extends readonly unknown[]> = unknown;
    

    Reveal the reference answer
    reference.typescript
    export 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.

  2. Exercise 2Implement

    Implement `Concat<A, B>` that concatenates two tuple types.

    starter.typescript
    export type Concat<A extends readonly unknown[], B extends readonly unknown[]> = [...A];
    

    Reveal the reference answer
    reference.typescript
    export 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.

  3. Exercise 3Test

    Write assertions proving `First` and `Concat` using `Equal` and `Expect`.

    starter.typescript
    export 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 here
    

    Reveal the reference answer
    reference.typescript
    export 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

Related resources

#

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.

© 2026 FrontendX. All rights reserved.