Indexed Access Types
Look up property types with the T[K] syntax.
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)All lessons in this course
- The typeof Type Operator
- The keyof Type Operator
- Indexed Access Types
- Combining typeof and keyof