Skip to main content
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.

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 as string or number when accessing index signatures or arrays), or a generic type parameter (e.g., K extends keyof T).

Mechanics and Behavior

1. Single Property Extraction

When IndexType is a single literal type, the resulting type is the exact type of that specific property on the TargetType.

2. Union Type Extraction

If IndexType 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 primitive number type.
  • Tuples: Indexing with a specific numeric literal extracts the type at that exact position.
  • Arrays/Tuples: Indexing with the primitive number type extracts a union of all possible element types within the array or tuple.

Strict Type Constraints

The IndexType passed into the brackets must be a valid type that exists as a key or index signature on the TargetType.
  1. 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').
  2. Values cannot be used as indices: You cannot pass a runtime variable into an indexed access type unless you extract its type using the typeof operator.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More