Indexed Access Types
Look up property types with the T[K] syntax.
Indexed Access Types is a free TypeScript Academy lesson on CoddyKit — lesson 3 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.
Looking Up Property Types
Indexed access types let you look up the type of a property using bracket syntax in a type position: Obj['prop']. It is like reading a value, but at the type level.
Basic Indexed Access
Index a type with a literal key to extract that property's type. User['name'] gives string.
interface User { id: number; name: string }
type NameType = User["name"] // string
const n: NameType = "Ada"
console.log(n)Why Not Just Write the Type
Indexed access keeps types in sync. If User.name changes from string to something else, every User['name'] updates automatically — no manual edits.
interface Product { price: { amount: number; currency: string } }
type Price = Product["price"] // { amount: number; currency: string }
const p: Price = { amount: 10, currency: "USD" }
console.log(p)Accessing With a Union of Keys
Index with a union of keys to get a union of their value types. User['id' | 'name'] yields number | string.
interface User { id: number; name: string }
type IdOrName = User["id" | "name"] // number | string
const v: IdOrName = 5
console.log(v)Obj[keyof Obj] for All Values
Index with keyof Obj to get a union of all value types in the object — its complete value-type set.
interface Config { host: string; port: number; secure: boolean }
type Values = Config[keyof Config] // string | number | boolean
const v: Values = true
console.log(v)Array Element Type With [number]
For an array type, index with number to get its element type. string[][number] is string.
type Names = string[]
type Item = Names[number] // string
const x: Item = "Ada"
console.log(x)Element Type of a Tuple
Indexing a tuple with number gives the union of all its element types — useful for deriving a value type from a fixed list.
type Pair = [string, number]
type Element = Pair[number] // string | number
const e: Element = 42
console.log(e)Combining With typeof
Index a value's type via typeof. Together with [number] on an as const array, this derives a union of the array's literal values.
const colors = ["red", "green", "blue"] as const
type Color = typeof colors[number] // "red" | "green" | "blue"
const c: Color = "green"
console.log(c)Nested Indexed Access
Chain lookups to dig into nested types. Each bracket descends one level into the structure.
interface Data { user: { profile: { age: number } } }
type Age = Data["user"]["profile"]["age"] // number
const a: Age = 36
console.log(a)Indexed Access in Generics
The pattern T[K] with K extends keyof T is the engine behind type-safe getters: the return type is exactly the looked-up property's type.
function get<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key]
}
const port = get({ host: "x", port: 80 }, "port")
console.log(port) // 80, typed as numberA Practical Derivation
Indexed access avoids duplicating types: derive a function-argument type straight from a domain object instead of restating it.
interface Order { items: { sku: string; qty: number }[] }
type LineItem = Order["items"][number] // { sku: string; qty: number }
const li: LineItem = { sku: "A1", qty: 2 }
console.log(li)Quick Check
Test your understanding of indexed access types.
Recap
Indexed access types (Obj['prop']) look up a property's type, staying in sync with the source. Index with a union of keys for a union of value types, with keyof Obj for all values, and with number for array or tuple element types. Chain brackets for nested lookups, and combine with typeof and generics (T[K]) for type-safe derivations.
Frequently asked questions
Is the “Indexed Access Types” lesson free?
Yes — the full text of “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 “Indexed Access Types”?
Look up property types with the T[K] syntax. 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 4, so you can start here or from the beginning and move at your own pace.
How long does the “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
- The typeof Type Operator
- The keyof Type Operator
- Indexed Access Types
- Combining typeof and keyof