0
0
Typescriptprogramming~10 mins

Type alias for objects in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type alias for objects
Define type alias
Use alias to type object
Create object with alias
Access object properties
Use object in code
First, you define a type alias for an object shape, then use it to type objects and access their properties.
Execution Sample
Typescript
type Person = {
  name: string;
  age: number;
};

const user: Person = { name: "Alice", age: 30 };
Defines a type alias 'Person' for an object with name and age, then creates a user object of that type.
Execution Table
StepActionEvaluationResult
1Define type alias PersonPerson = {name: string, age: number}Type alias created
2Declare user with type Personuser: PersonVariable user typed as Person
3Assign object {name: "Alice", age: 30} to useruser = {name: "Alice", age: 30}Object matches Person type
4Access user.nameuser.name"Alice"
5Access user.ageuser.age30
💡 All steps complete, object created and properties accessed successfully.
Variable Tracker
VariableStartAfter 3Final
Personundefined{name: string, age: number}{name: string, age: number}
userundefined{name: "Alice", age: 30}{name: "Alice", age: 30}
Key Moments - 2 Insights
Why do we use a type alias instead of typing the object inline?
Using a type alias (see Step 1 in execution_table) lets us reuse the object shape easily and keeps code clean.
What happens if the object does not match the type alias?
TypeScript will show an error at Step 3 because the object must have all properties with correct types as defined in the alias.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of user.name at Step 4?
Aundefined
B"Bob"
C"Alice"
D30
💡 Hint
Check the 'Evaluation' and 'Result' columns at Step 4 in the execution_table.
At which step is the type alias 'Person' created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column to find when the alias is defined.
If we remove the 'age' property from the object assigned to user, what happens?
ANo error, object is accepted
BTypeScript error at Step 3
CError at Step 4 when accessing user.name
Duser.age becomes undefined
💡 Hint
Refer to the key moment about object matching the type alias and Step 3 in execution_table.
Concept Snapshot
Type alias for objects:
- Use 'type Name = { prop: type; }' to define shape
- Use alias to type variables: 'const x: Name'
- Helps reuse and clarity
- Object must match alias shape
- Access properties normally
Full Transcript
This example shows how to create a type alias named Person for an object with name and age properties. Then, we declare a variable user of type Person and assign an object with matching properties. We access user.name and user.age to get their values. The execution table traces each step: defining the alias, declaring the variable, assigning the object, and accessing properties. The variable tracker shows how Person and user change from undefined to their final values. Key moments clarify why type aliases are useful and what happens if the object does not match the alias. The quiz tests understanding of values at steps and error conditions. This helps beginners see how TypeScript uses type aliases for objects clearly and step-by-step.