An indexed access type (often referred to as a lookup type) is a TypeScript mechanism used to retrieve the type of a specific property or a set of properties from another type. It allows developers to dynamically reference the type of a property by using bracket notation, operating strictly at the type level rather than the value level.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Syntax
TargetType: The object, array, or tuple type being queried.IndexType: The type representing the key(s) to look up. This can be a string, numeric, or symbol literal type, a union of literal types, a primitive type (such asstringornumberwhen accessing index signatures or arrays), or a generic type parameter (e.g.,K extends keyof T).
Mechanics and Behavior
1. Single Property Extraction
WhenIndexType is a single literal type, the resulting type is the exact type of that specific property on the TargetType.
2. Union Type Extraction
IfIndexType is a union of types, TypeScript evaluates the lookup for each member of the union and returns a union of the corresponding property types.
3. Exhaustive Extraction via keyof
Combining an indexed access type with the keyof operator extracts a union type representing all possible value types within the TargetType.
4. Array and Tuple Extraction
Indexed access types can be applied to arrays and tuples using numeric literal types or the primitivenumber type.
- Tuples: Indexing with a specific numeric literal extracts the type at that exact position.
- Arrays/Tuples: Indexing with the primitive
numbertype extracts a union of all possible element types within the array or tuple.
Strict Type Constraints
TheIndexType passed into the brackets must be a valid type that exists as a key or index signature on the TargetType.
- Invalid indices yield errors: Attempting to use a type that cannot index the target type will result in a compiler error (
Type 'X' cannot be used to index type 'Y'). - Values cannot be used as indices: You cannot pass a runtime variable into an indexed access type unless you extract its type using the
typeofoperator.
Master TypeScript with Deep Grasping Methodology!Learn More





