What if you could ask your code to tell you exactly what type a property has, without guessing or repeating yourself?
Why Indexed access types in Typescript? - Purpose & Use Cases
Imagine you have a big box of different toys, and you want to pick a specific toy by its label every time. Without a clear way to look up the toy by its label, you have to search through the whole box manually each time.
Manually checking each toy's label is slow and can lead to mistakes, like picking the wrong toy or forgetting which labels exist. It's hard to keep track and easy to get confused when the box gets bigger.
Indexed access types let you directly ask for the type of a specific property inside an object type by using its key. This means you can quickly and safely get the exact type you want without guessing or repeating yourself.
type User = { name: string; age: number };
type UserName = string; // manually repeating the typetype User = { name: string; age: number };
type UserName = User['name']; // directly gets the type of 'name'It makes your code smarter and safer by letting you reuse and check types automatically based on existing structures.
When building a form, you can use indexed access types to ensure the input fields match the exact data types defined in your user profile, avoiding mistakes and extra work.
Manual type repetition is slow and error-prone.
Indexed access types let you get property types directly from objects.
This keeps your code clean, safe, and easy to maintain.
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
