Indexed Access 类型
一句话解释
indexed access 类型 (T[K]) extracts 类型的 属性 K 从类型 T, 启用类型-level 属性 lookup.
用大白话说
indexed access 类型 T[K] 获取类型的 属性 K 从类型 T. For interface Person { name: string; age: number }, Person["name"] 是 string and Person["age"] 是 number.
This is the type-level equivalent of obj[键] at the value level. Combined with keyof, it enables generic functions that access properties with full type safety.
Indexed access types are used in mapped types, utility types, and generic functions. They are essential for type-level programming — deriving types from other types automatically.
示例
typescript
interface User { id: number; name: string; roles: string[]; }
type IdType = User["id"]; // number
type NameType = User["name"]; // string
type RolesType = User["roles"]; // string[]Indexed access 类型 extract 类型的 每个属性从 用户接口.
相关术语
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。