Indexed access types let you get the type of a specific property from another type. This helps you reuse types easily without repeating them.
Indexed access types in Typescript
type NewType = ExistingType['propertyName'];You use square brackets with the property name as a string inside to get the type.
You can also use union of keys like ExistingType['prop1' | 'prop2'] to get combined types.
name property from Person, which is string.type Person = { name: string; age: number };
type NameType = Person['name'];age or name, so the type is number | string.type Person = { name: string; age: number };
type AgeOrName = Person['age' | 'name'];make property from the Car interface.interface Car {
make: string;
year: number;
}
type MakeType = Car['make'];This program shows how to use indexed access types to get property types from the User interface. We assign values matching those types and print them.
interface User {
id: number;
username: string;
isActive: boolean;
}
// Get the type of the 'username' property
let userName: User['username'];
userName = 'alice123';
// Get the type of 'id' or 'isActive'
let userInfo: User['id' | 'isActive'];
userInfo = 42; // valid
userInfo = true; // also valid
console.log(userName);
console.log(userInfo);Indexed access types help keep your types consistent and avoid mistakes.
If you use a property name that does not exist, TypeScript will show an error.
You can combine indexed access types with other TypeScript features for powerful type manipulation.
Indexed access types let you get the type of a property from another type.
Use square brackets with the property name to extract the type.
This helps reuse types and keep your code clean and safe.
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
