indexed access type
A type written T[K] that looks up the type of property K on type T, such as UserEntity["email"] giving string.
In more detail
Combined with keyof it returns the exact type of whichever property was requested. typeof routes[0]["path"] reads a type out of a value the same way.
Example
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K]Related terms
- keyof — The TypeScript type operator that produces a union of an object type's property names, such as "id" | "table" | "rowCount".
- generic constraint — A rule on a type parameter that limits which types may fill it, such as requiring a class, a particular interface or a certain shape.
- typeof — A keyword that asks for the type of a value, both in JavaScript at run time and in TypeScript types.
Taught in
- tsconfig.json: target, lib, strict And NodeNext — Production TypeScript
- Optional Properties, Readonly And Index Signatures — Production TypeScript
- Generic Constraints: extends, keyof And Indexed Access — Production TypeScript
- Const Type Parameters: Retaining Literal Types In Functions — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript