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 Basics
interface User { id: number; name: string; email: string; }
type UserKeys = keyof User; // 'id' | 'name' | 'email'Indexed Access: T[K]
type IdType = User['id']; // number
type NameType = User['name']; // stringDynamic Property Access
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const name = getProperty(user, 'name'); // stringkeyof with typeof
const config = { host: 'localhost', port: 3000 };
type ConfigKey = keyof typeof config; // 'host' | 'port'Union of Indexed Types
type NumericValues = User['id' | 'age']; // number | number = numberkeyof on Arrays
type ArrKey = keyof string[]; // number | 'length' | 'push' | ...
type Element = string[][number]; // stringRecursive Indexed Access
type Address = { city: string; zip: string };
type Profile = { address: Address };
type City = Profile['address']['city']; // stringConstraints with keyof
function setProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]): void {
obj[key] = value;
}Mapped Types Use keyof
type Readonly2<T> = { readonly [K in keyof T]: T[K] };keyof in Conditional Types
type StringKeys<T> = { [K in keyof T]: T[K] extends string ? K : never }[keyof T];Quick Check
Recap
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
- keyof and Indexed Access Types
- Generic Constraints: Narrowing Type Parameters
- Conditional Types: T extends U ? X : Y
- Distributive Conditional Types