0
0
Typescriptprogramming~10 mins

Optional properties in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Optional properties
Define interface with optional property
Create object with or without optional property
Access optional property
Check if property exists before use
Use property safely or handle undefined
This flow shows how optional properties in TypeScript interfaces allow objects to have or skip certain properties, and how to safely access them.
Execution Sample
Typescript
interface User {
  name: string;
  age?: number;
}

const user1: User = { name: "Alice" };
const user2: User = { name: "Bob", age: 30 };

console.log(user1.age);
console.log(user2.age);
This code defines a User interface with an optional age property, creates two users (one without age), and logs their ages.
Execution Table
StepActionObject StateProperty AccessOutput
1Define interface User with optional ageUser { name: string; age?: number }N/AN/A
2Create user1 with name onlyuser1 = { name: "Alice" }user1.ageundefined
3Create user2 with name and ageuser2 = { name: "Bob", age: 30 }user2.age30
4Log user1.ageuser1 = { name: "Alice" }user1.ageundefined
5Log user2.ageuser2 = { name: "Bob", age: 30 }user2.age30
💡 All steps executed; optional property age is undefined for user1 and defined for user2.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
user1undefined{ name: "Alice" }{ name: "Alice" }{ name: "Alice" }
user2undefinedundefined{ name: "Bob", age: 30 }{ name: "Bob", age: 30 }
Key Moments - 2 Insights
Why does user1.age show undefined instead of an error?
Because age is marked optional with ?, it may be missing. Accessing it returns undefined, not an error, as shown in execution_table step 4.
Can we assign user1.age later even if it was not set initially?
Yes, optional means the property can be missing initially but can be added later. The interface allows it to be undefined or a number.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when accessing user1.age at step 4?
Aundefined
Bnull
C0
DError
💡 Hint
Check the 'Output' column for step 4 in execution_table.
At which step is user2 created with the age property defined?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Object State' columns in execution_table.
If we remove the ? from age in the interface, what would happen when creating user1 without age?
Auser1.age would be undefined
BIt would cause a TypeScript error
Cuser1 would have age set to null
DNo change, code runs fine
💡 Hint
Optional properties allow missing fields; removing ? makes age required.
Concept Snapshot
interface User {
  name: string;
  age?: number; // optional property
}

- Optional properties may be missing
- Accessing missing optional property returns undefined
- Helps create flexible object shapes
Full Transcript
This example shows how TypeScript interfaces can have optional properties using the ? symbol. The User interface has an optional age property. We create two objects: user1 without age and user2 with age. Accessing user1.age returns undefined safely without error. This allows flexible objects where some properties can be missing. The execution table traces each step: defining interface, creating objects, and accessing properties. The variable tracker shows how user1 and user2 change. Key moments clarify why accessing missing optional properties does not cause errors. The quiz tests understanding of optional properties behavior and TypeScript rules.