keyof/indexed access types (intro)
Use keyof to create unions of property names and indexed access types (T[K] / T["prop"]) to read property types safely.
keyof/indexed access types (intro) is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Build safer helpers using keyof (property-name unions) and indexed access (T[K]) to read property types.
keyof
keyof produces a union of property names of a type (e.g., "id" | "name" | "email").
interface User {
id: number;
name: string;
email?: string;
}
type UserKeys = keyof User; // "id" | "name" | "email"keyof typeof
Use keyof typeof to get keys from a value (object) for parameter typing.
const SETTINGS = {
theme: "dark",
lang: "en",
} as const;
type SettingKey = keyof typeof SETTINGS; // "theme" | "lang"Indexed access
Indexed access returns the type of a property: User["name"] is string.
interface User {
id: number;
name: string;
email?: string;
}
type NameType = User["name"]; // string
getProperty helper
Combine keyof with K extends keyof T to restrict keys and return the precise T[K] type.
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const u = { id: 1, name: "Ada" };
const v1 = getProperty(u, "name"); // string
const v2 = getProperty(u, "id"); // number
console.log(v1.toUpperCase(), v2.toFixed(0));Tips
Tips:
- Prefer keyof typeof for keys from objects.
- Use T[K] to reflect property types without duplicating models.
- Avoid stringly-typed keys; lean on unions for safety.
Indexed access check
Quick check: What does T[K] represent?
Recap
Recap: keyof gives key unions; keyof typeof derives keys from values; T[K] reads property types—perfect for safe helpers.
Frequently asked questions
Is the “keyof/indexed access types (intro)” lesson free?
Yes — the full text of “keyof/indexed access types (intro)” is free to read here on the web, and the TypeScript Academy course includes 3 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/indexed access types (intro)”?
Use keyof to create unions of property names and indexed access types (T[K] / T["prop"]) to read property types safely. 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 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “keyof/indexed access types (intro)” 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
- Generic functions & constraints (extends, default params)
- Partial, Readonly, Record, Pick, Omit
- keyof/indexed access types (intro)