Skip to main content

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.

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

type ExtractedType = TargetType[IndexType];
  • 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.
type Configuration = {
  endpoint: string;
  timeout: number;
  secure: boolean;
};

type TimeoutType = Configuration["timeout"]; 
// Result: number

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.
type EndpointOrSecure = Configuration["endpoint" | "secure"]; 
// Result: string | boolean

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.
type AllConfigurationValues = Configuration[keyof Configuration]; 
// Result: string | number | boolean

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.
type DataTuple = [string, number, boolean];

type FirstElement = DataTuple[0]; 
// Result: string

type AnyTupleElement = DataTuple[number]; 
// Result: string | number | boolean

type StringArray = string[];
type ArrayElement = StringArray[number]; 
// Result: string

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.
const key = "timeout";

// ERROR: 'key' refers to a value, but is being used as a type here.
type InvalidLookup = Configuration[key]; 

// VALID: Extracts the literal type "timeout" from the constant value.
type ValidLookup = Configuration[typeof key]; 
Master TypeScript with Deep Grasping Methodology!Learn More