Codington
0 of 6 problems solved0%

keyof, typeof & Indexed Access

6 exercises

This category links runtime values to static types using powerful type operators:

keyof – Get all property names as a union:

type User = { id: number; name: string }; type UserKeys = keyof User; // "id" | "name"

T[K] – Indexed access to look up property types:

type User = { id: number; name: string }; type IdType = User["id"]; // number

typeof – Turn a runtime value into a type:

const config = { apiUrl: "...", timeout: 5000 }; type Config = typeof config; // { apiUrl: string; timeout: number }

as const – Preserve literal types:

const colors = ['red', 'green', 'blue'] as const; type Color = typeof colors[number]; // "red" | "green" | "blue"

In this category, you'll:

  • Use keyof to get property names safely
  • Look up property types with indexed access T[K]
  • Convert runtime values to types with typeof
  • Preserve literals with as const
  • Combine these tools to connect values and types

Each exercise focuses on practical type connections while keeping the runtime code small and clear.

CodingtonCodington

© 2025 Codington. Built with TypeScript, TanStack Start and a lot of console.log().