Indexed access types in Typescript - Time & Space Complexity
We want to understand how the time it takes to access types using indexed access changes as the input grows.
Specifically, how does the cost grow when we use indexed access types in TypeScript?
Analyze the time complexity of the following code snippet.
type Person = {
name: string;
age: number;
location: string;
};
// Access the type of the 'age' property
type AgeType = Person['age'];
// Access the type of a union of keys
type NameOrLocation = Person['name' | 'location'];
This code uses indexed access types to get the type of specific properties from an object type.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Type lookup for each key in the union inside the indexed access.
- How many times: Once per key in the union type (for example, 1 for 'age', 2 for 'name' | 'location').
As the number of keys in the union grows, the number of type lookups grows linearly.
| Input Size (number of keys) | Approx. Operations (type lookups) |
|---|---|
| 1 | 1 |
| 10 | 10 |
| 100 | 100 |
Pattern observation: The work grows directly with the number of keys accessed.
Time Complexity: O(n)
This means the time to resolve the indexed access type grows linearly with the number of keys in the union.
[X] Wrong: "Indexed access types always take constant time regardless of keys."
[OK] Correct: When accessing multiple keys in a union, each key requires a separate lookup, so time grows with the number of keys.
Understanding how type operations scale helps you reason about code maintainability and performance in TypeScript projects.
"What if we changed the union of keys to a single key type? How would the time complexity change?"
Practice
Person['name'] represent?Solution
Step 1: Understand indexed access syntax
The syntaxType['property']extracts the type of the specified property from the given type.Step 2: Apply to Person['name']
This meansPerson['name']is the type of the 'name' property inside the Person type.Final Answer:
The type of the 'name' property in the Person type -> Option BQuick Check:
Indexed access type = property type [OK]
- Thinking it creates a new property
- Confusing it with a function call
- Assuming it causes a syntax error
age from type Person using indexed access types?Solution
Step 1: Recall indexed access syntax
The correct syntax uses square brackets with the property name as a string:Type['property'].Step 2: Check each option
type AgeType = Person['age']; matches this syntax exactly:Person['age']. Others use dot notation or parentheses which are invalid here.Final Answer:
type AgeType = Person['age']; -> Option AQuick Check:
Use square brackets with quotes for indexed access [OK]
- Using dot notation instead of brackets
- Using parentheses like a function call
- Using angle brackets incorrectly
type Person = { name: string; age: number; }, what is the type of Person['age']?Solution
Step 1: Look at the Person type definition
The property 'age' in Person is defined as a number.Step 2: Indexed access extracts that type
SoPerson['age']is the type number.Final Answer:
number -> Option DQuick Check:
Person['age'] = number [OK]
- Confusing 'age' type with 'name' type
- Assuming default any type
- Mixing up string and number types
type Person = { name: string; age: number; };
type AgeType = Person[age];Solution
Step 1: Identify the indexed access syntax error
InPerson[age], 'age' should be a string literal in quotes:Person['age'].Step 2: Understand why quotes are needed
Without quotes, TypeScript treatsageas a variable, which is undefined here, causing an error.Final Answer:
Missing quotes around 'age' in indexed access -> Option CQuick Check:
Property names in indexed access need quotes [OK]
- Omitting quotes around property names
- Assuming dot notation works in type access
- Confusing variables with string literals
type Config = { options: { debug: boolean; verbose: boolean; }; version: string; }, which type correctly extracts the type of the debug property using indexed access types?Solution
Step 1: Understand nested indexed access
The 'debug' property is inside 'options', so we first access 'options' then 'debug'.Step 2: Apply chained indexed access syntax
UseConfig['options']['debug']to get the type of 'debug'.Step 3: Check other options
Config.options.debug uses dot notation which is invalid in type access. Config['debug'] tries to access 'debug' directly on Config which doesn't exist. Config['options.debug'] treats 'options.debug' as a single property name which is incorrect.Final Answer:
Config['options']['debug'] -> Option AQuick Check:
Use chained brackets for nested properties [OK]
- Using dot notation in type access
- Accessing nested property directly without parent
- Treating nested keys as single string
Ready to prove it?
Pass the test and this pattern moves unknown
