Skip to article content
FrontendXLesson 09 / 40

keyof, indexed access and satisfies

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

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Use keyof and typeof to derive key unions.
  • Read property types with indexed access.
  • Prefer satisfies over a widening annotation for config objects.

Overview

#

keyof

keyof T is the union of T's keys.

snippet.ts
type User = { id: string; name: string };
type UserKey = keyof User; // "id" | "name"

typeof in type position

typeof value reads the type of a runtime binding.

snippet.ts
const routes = { home: "/", about: "/about" } as const;
type RouteName = keyof typeof routes; // "home" | "about"

Indexed access

T[K] is the type of the property K.

snippet.ts
type NameType = User["name"]; // string
type AnyValue = User[keyof User]; // string

as const

as const makes literals stay literal and arrays become readonly tuples. It is how you keep a config's exact shape.

satisfies

An annotation widens the value to the declared type. satisfies checks the value against a type without changing its inferred type.

snippet.ts
type Palette = Record<string, [number, number, number]>;

const palette = {
  red: [255, 0, 0],
  green: [0, 255, 0],
} satisfies Palette;

palette.red[0]; // number, and palette.green is a known key

With a plain annotation, palette.red would still work but the exact keys would be lost for autocomplete and excess key checks would differ.

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 `get<T, K extends keyof T>(obj: T, key: K): T[K]` with a fallback overload that returns a default when the key may be missing.

    starter.typescript
    export function get<T, K extends keyof T>(obj: T, key: K): T[K];
    export function get<T, K extends keyof T, D>(obj: T, key: K, fallback: D): T[K] | D;
    export function get<T, K extends keyof T, D>(obj: T, key: K, fallback?: D): T[K] | D | undefined {
      // your code
    }
    

    Reveal the reference answer
    reference.typescript
    export function get<T, K extends keyof T>(obj: T, key: K): T[K];
    export function get<T, K extends keyof T, D>(obj: T, key: K, fallback: D): T[K] | D;
    export function get<T, K extends keyof T, D>(obj: T, key: K, fallback?: D): T[K] | D | undefined {
      const value = obj[key];
      return value === undefined ? fallback : value;
    }
    

    Authored expected output: Compiles with no errors. Runtime: get({ a: 1 }, "a") is 1; get({} as { a?: number }, "a", 0) is 0.

    The overloads give a precise return type with and without a fallback.

  2. Exercise 2Predict

    What is the type of `keys`? ```ts const sizes = { sm: 1, md: 2, lg: 3 } as const; type Keys = keyof typeof sizes; ```

    Choose the best answer
  3. Exercise 3Implement

    Write a config object using `satisfies` so that each route value is a string starting with a slash, then derive a union of its keys.

    starter.typescript
    export const routes = {
      home: "/",
      about: "/about",
    };
    
    export type RouteName = never;
    

    Reveal the reference answer
    reference.typescript
    export const routes = {
      home: "/",
      about: "/about",
    } satisfies Record<string, `/${string}`>;
    
    export type RouteName = keyof typeof routes;
    

    Authored expected output: Compiles with no errors. RouteName is "home" | "about"; a value like "about" without a leading slash fails to compile.

    satisfies checks each value against the template literal type while keeping the exact keys.

  4. Exercise 4Debug

    The annotation below widens the value and loses key information. Rewrite it with satisfies so `theme` stays a literal union. ```ts type Theme = "light" | "dark"; const config: Record<string, Theme> = { theme: "dark" }; const t = config.theme; // Theme, but keys are lost ```

    starter.typescript
    type Theme = "light" | "dark";
    const config: Record<string, Theme> = { theme: "dark" };
    const t = config.theme;
    

    Reveal the reference answer
    reference.typescript
    type Theme = "light" | "dark";
    const config = { theme: "dark" } satisfies Record<string, Theme>;
    const t = config.theme; // "dark"
    

    Authored expected output: Compiles with no errors. config.theme is now the literal "dark" and config keys are known.

    satisfies validates without widening, so the inferred literal type is preserved.

Common misconceptions

#
  • keyof returns the values of an object.
  • satisfies widens the value like an annotation.
  • as const is needed for every object literal.

Follow-up practice

#
  • typescript-conditional-infer

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.