Skip to article content
FrontendXLesson 10 / 40

Conditional types and infer

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 50 minutes · 4 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Write a conditional type with extends.
  • Extract types with infer.
  • Explain distributive conditional types over unions.

Overview

#

Conditional types

A conditional type chooses between two types based on assignability.

snippet.ts
type IsString<T> = T extends string ? true : false;

This is a compile-time decision. No runtime branch is created.

infer

infer introduces a type variable inside a conditional and lets the checker solve for it.

snippet.ts
type ElementType<T> = T extends Array<infer U> ? U : never;
type Unwrap<T> = T extends Promise<infer U> ? U : T;

Distributive behavior

When the checked type is a naked type parameter, the conditional distributes over unions.

snippet.ts
type ToArray<T> = T extends unknown ? T[] : never;
type R = ToArray<string | number>; // string[] | number[]

Wrapping the parameter in a tuple stops distribution: [T] extends [unknown] ? T[] : never gives (string | number)[].

Built-ins built this way

Exclude, Extract, NonNullable and Awaited are all conditional types. The next exercise rebuilds one from scratch.

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 `MyExclude<T, U>` that removes from T every member assignable to U, without using the built-in Exclude.

    starter.typescript
    export type MyExclude<T, U> = T;
    

    Reveal the reference answer
    reference.typescript
    export type MyExclude<T, U> = T extends U ? never : T;
    

    Authored expected output: Compile-time: MyExclude<"a" | "b" | "c", "a"> is "b" | "c". No runtime output.

    The naked T distributes over the union and drops members assignable to U.

  2. Exercise 2Implement

    Implement `UnwrapPromise<T>` that resolves a promise type to its value type and leaves non-promises unchanged, using infer.

    starter.typescript
    export type UnwrapPromise<T> = T;
    

    Reveal the reference answer
    reference.typescript
    export type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
    

    Authored expected output: Compile-time: UnwrapPromise<Promise<number>> is number; UnwrapPromise<string> is string. No runtime output.

    infer names the resolved type inside the conditional.

  3. Exercise 3Predict

    What is `R`? ```ts type ToArray<T> = T extends unknown ? T[] : never; type R = ToArray<string | number>; ```

    Choose the best answer
  4. Exercise 4Debug

    This conditional distributes when it should not. Fix it so `R` is `(string | number)[]`. ```ts type ToArray<T> = T extends unknown ? T[] : never; type R = ToArray<string | number>; ```

    starter.typescript
    type ToArray<T> = T extends unknown ? T[] : never;
    type R = ToArray<string | number>;
    

    Reveal the reference answer
    reference.typescript
    type ToArray<T> = [T] extends [unknown] ? T[] : never;
    type R = ToArray<string | number>;
    

    Authored expected output: Compile-time: R is (string | number)[]. No runtime output.

    Wrapping both sides in tuples makes the check non-distributive.

Common misconceptions

#
  • Conditional types run at runtime.
  • infer must be named to match an existing type.
  • Conditionals never distribute.

Follow-up practice

#
  • typescript-mapped-template

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.