0
0
Typescriptprogramming~10 mins

Indexed access types in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Indexed access types
Define object type
Use indexed access type: Type[Key
Extract property type
Use extracted type in variable or function
Indexed access types let you get the type of a property from an object type using its key.
Execution Sample
Typescript
type Person = { name: string; age: number };
let personName: Person['name'];
personName = "Alice";
This code extracts the type of 'name' from Person and uses it for personName.
Execution Table
StepActionEvaluationResult
1Define type PersonPerson = { name: string; age: number }Person type created
2Use indexed access type Person['name']Person['name']string
3Declare variable personName with type Person['name']let personName: Person['name']Variable declared
4Assign "Alice" to personNamepersonName = "Alice"personName = "Alice"
5Use personName as stringtypeof personName"string"
💡 All steps complete, personName holds a string as expected
Variable Tracker
VariableStartAfter 1After 2After 3Final
personNameundefinedundefinedundefinedundefined"Alice"
Key Moments - 2 Insights
Why does Person['name'] give the type string?
Because Person defines 'name' as string, so indexed access extracts that exact type (see execution_table step 2).
Can I assign a number to personName?
No, personName is typed as string from Person['name'], so assigning a number would cause a type error (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type does Person['age'] represent?
Astring
Bnumber
Cboolean
Dany
💡 Hint
Check execution_table step 2 where Person['name'] is string; similarly, Person['age'] is number.
At which step is the variable personName assigned a value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table step 4 where personName is assigned "Alice".
If Person['name'] was changed to number, what would happen to personName's type?
AIt becomes number
BIt stays string
CIt becomes any
DIt causes a syntax error
💡 Hint
Indexed access types reflect the property type exactly, so changing Person['name'] changes personName's type (see concept_flow).
Concept Snapshot
Indexed access types syntax: TypeName[Key]
Extracts the type of a property from an object type.
Useful to reuse property types without repeating.
Example: type NameType = Person['name'] gives string.
Keeps types consistent and DRY.
Full Transcript
Indexed access types in TypeScript let you get the type of a property from an object type by using the property key in square brackets. For example, if you have a type Person with properties name and age, you can get the type of name by writing Person['name'], which is string. This helps you reuse types without repeating them. In the example, we declare a variable personName with type Person['name'], so personName must be a string. Assigning a string to personName works fine. This visual trace shows each step: defining the type, extracting the property type, declaring the variable, assigning a value, and confirming the type. Remember, if the property type changes, the indexed access type changes too, keeping your code consistent.