0
0
Typescriptprogramming~10 mins

Optional properties in interfaces in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Optional properties in interfaces
Define Interface with Optional Property
Create Object with or without Optional Property
Access Property
Property Exists
Use Value
This flow shows how an interface with optional properties allows objects to include or omit those properties, and how code handles their presence or absence.
Execution Sample
Typescript
interface Person {
  name: string;
  age?: number;
}

const p1: Person = { name: "Alice" };
const p2: Person = { name: "Bob", age: 30 };
Defines a Person interface with an optional age property, then creates two objects: one without age and one with age.
Execution Table
StepActionObject CreatedProperty 'age' Present?Value of 'age'
1Create p1 with only name{ name: "Alice" }Noundefined
2Access p1.ageSame as aboveNoundefined
3Create p2 with name and age{ name: "Bob", age: 30 }Yes30
4Access p2.ageSame as aboveYes30
💡 All objects created successfully; optional property 'age' may be missing or present.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
p1undefined{ name: "Alice" }{ name: "Alice" }{ name: "Alice" }
p2undefinedundefined{ name: "Bob", age: 30 }{ name: "Bob", age: 30 }
Key Moments - 2 Insights
Why can we create p1 without the 'age' property even though it's in the interface?
Because 'age' is marked optional with '?', it means objects can omit it. The execution_table rows 1 and 2 show p1 created without 'age' and accessing it returns undefined.
What happens if we try to access an optional property that is missing?
Accessing a missing optional property returns undefined, as shown in execution_table row 2 when accessing p1.age.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p1.age after step 2?
A0
Bundefined
Cnull
D30
💡 Hint
Check the 'Value of age' column in row 2 of the execution_table.
At which step does the object have the 'age' property present?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Property age Present?' column in the execution_table.
If we remove the '?' from 'age' in the interface, what would happen when creating p1?
Ap1 creation would cause a type error
Bp1 would be created successfully without age
Cp1.age would be undefined but no error
Dp1.age would default to 0
💡 Hint
Optional properties allow omission; removing '?' makes 'age' required.
Concept Snapshot
interface Person {
  name: string;
  age?: number; // '?' means optional
}

- Objects can omit optional properties.
- Accessing missing optional properties returns undefined.
- Helps flexible object shapes without errors.
Full Transcript
This example shows how to use optional properties in TypeScript interfaces. The interface Person has a required 'name' and an optional 'age'. We create two objects: p1 without 'age' and p2 with 'age'. Accessing p1.age returns undefined because it is missing. Optional properties allow objects to be flexible and avoid errors when some data is not present.