Skip to article content
FrontendXLesson 11 / 40

Mapped and template literal types

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

  • Implement
  • Interview

What you will be able to do

#
  • Write a mapped type over keyof T.
  • Add or remove readonly and optional modifiers.
  • Rename keys with as and build string types with template literals.

Overview

#

Mapped types

A mapped type iterates the keys of another type.

snippet.ts
type MyPartial<T> = { [K in keyof T]?: T[K] };
type MyReadonly<T> = { readonly [K in keyof T]: T[K] };

The [K in keyof T] clause visits each key and T[K] reads its value type.

Modifiers

+? adds optional, -? removes it. +readonly adds readonly, -readonly removes it.

snippet.ts
type Concrete<T> = { [K in keyof T]-?: T[K] };
type Mutable<T> = { -readonly [K in keyof T]: T[K] };

Key remapping

as transforms the key, and template literal types build the new name.

snippet.ts
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};

Capitalize and its siblings (Uppercase, Lowercase, Uncapitalize) are intrinsic string types.

Template literal types

A template literal type composes string types.

snippet.ts
type EventName<T extends string> = `on${Capitalize<T>}`;
type Click = EventName<"click">; // "onClick"

Power and cost

The checker evaluates these types, so deeply recursive mapped types can slow down an editor. Keep them shallow and purposeful.

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 `MyPartial<T>` from scratch with a mapped type, without using the built-in Partial.

    starter.typescript
    export type MyPartial<T> = { [K in keyof T]: T[K] };
    

    Reveal the reference answer
    reference.typescript
    export type MyPartial<T> = { [K in keyof T]?: T[K] };
    

    Authored expected output: Compile-time: MyPartial<{ a: number; b: string }> is { a?: number; b?: string }. No runtime output.

    The mapped type copies each key and adds the optional modifier.

  2. Exercise 2Implement

    Implement `Getters<T>` that maps each key K of T to a method named `get` plus the capitalized key, returning T[K].

    starter.typescript
    export type Getters<T> = {
      [K in keyof T]: () => T[K];
    };
    

    Reveal the reference answer
    reference.typescript
    export type Getters<T> = {
      [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
    };
    

    Authored expected output: Compile-time: Getters<{ name: string }> is { getName: () => string }. No runtime output.

    Key remapping with a template literal renames each key while preserving its value type.

  3. Exercise 3Implement

    Implement `EventName<T extends string>` that prefixes `on` and capitalizes, then derive the handler name for "click".

    starter.typescript
    export type EventName<T extends string> = Capitalize<T>;
    export type ClickHandler = EventName<"click">;
    

    Reveal the reference answer
    reference.typescript
    export type EventName<T extends string> = `on${Capitalize<T>}`;
    export type ClickHandler = EventName<"click">;
    

    Authored expected output: Compile-time: ClickHandler is "onClick". No runtime output.

    The template literal type composes a fixed prefix with the capitalized input.

  4. Exercise 4Interview

    Explain how you would build a type that turns every field of a form model into an optional, string-typed input value, and why the mapped type is preferable to writing the shape by hand.

    Reveal the reference answer
    reference.typescript
    Map over keyof T with a mapped type, adding the optional modifier and overriding the value type to string: { [K in keyof T]?: string }. This derives the input shape from the single source of truth, so adding a field to the model automatically adds the input and removing one removes it. Hand-writing the shape drifts from the model and produces silent mismatches.

    Authored expected output: A design answer naming a mapped type over keyof and the single-source-of-truth benefit.

    Mapped types keep derived shapes in sync with their source.

Common misconceptions

#
  • Mapped types change values at runtime.
  • as in a mapped type is a type assertion.
  • Template literal types accept any expression.

Follow-up practice

#
  • typescript-type-challenge-capstone

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.