0Pricing
TypeScript Academy · Lesson

keyof and Indexed Access Types

Access object property types dynamically with keyof and T[K].

keyof and Indexed Access Types is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

keyof produces a union of an object type's property names. Indexed access types (T[K]) retrieve the type at a specific property key.

keyof Basics

keyof T returns a union of all property name types of T.
interface User { id: number; name: string; email: string; }
type UserKeys = keyof User; // 'id' | 'name' | 'email'

Indexed Access: T[K]

Use T[K] to get the type of property K in type T.
type IdType = User['id'];   // number
type NameType = User['name']; // string

Dynamic Property Access

Combine keyof and generics to build type-safe property accessor functions.
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}
const name = getProperty(user, 'name'); // string

keyof with typeof

Use typeof on a value and then keyof to get its property names without declaring an interface.
const config = { host: 'localhost', port: 3000 };
type ConfigKey = keyof typeof config; // 'host' | 'port'

Union of Indexed Types

Index a type with a union of keys to get the union of their value types.
type NumericValues = User['id' | 'age']; // number | number = number

keyof on Arrays

keyof on an array type returns array index types plus array method names.
type ArrKey = keyof string[]; // number | 'length' | 'push' | ...
type Element = string[][number]; // string

Recursive Indexed Access

Chain indexed access types for nested objects.
type Address = { city: string; zip: string };
type Profile = { address: Address };
type City = Profile['address']['city']; // string

Constraints with keyof

Use K extends keyof T as a generic constraint to ensure K is a valid key of T.
function setProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]): void {
  obj[key] = value;
}

Mapped Types Use keyof

Mapped types iterate over keyof T to transform object types.
type Readonly2<T> = { readonly [K in keyof T]: T[K] };

keyof in Conditional Types

keyof is powerful in conditional types for extracting specific key groups.
type StringKeys<T> = { [K in keyof T]: T[K] extends string ? K : never }[keyof T];

Quick Check

What does `User['name']` return if `interface User { name: string }`?

Recap

keyof extracts property names as a union type. T[K] retrieves the type at a key. Together they power type-safe property access, mapped types, and conditional type utilities.

Frequently asked questions

Is the “keyof and Indexed Access Types” lesson free?

Yes — the full text of “keyof and Indexed Access Types” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “keyof and Indexed Access Types”?

Access object property types dynamically with keyof and T[K]. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “keyof and Indexed Access Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. keyof and Indexed Access Types
  2. Generic Constraints: Narrowing Type Parameters
  3. Conditional Types: T extends U ? X : Y
  4. Distributive Conditional Types
← Back to TypeScript Academy