0
0
Typescriptprogramming~10 mins

What structural typing means in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - What structural typing means
Define Type A with properties
Create object with matching properties
Assign object to Type A variable
TypeScript checks if object shape matches Type A
If shapes match, assignment allowed
Use object as Type A safely
Structural typing means TypeScript checks if an object has the right shape (properties and types) to match a type, not if it explicitly declares that type.
Execution Sample
Typescript
interface Point { x: number; y: number; }
const p = { x: 10, y: 20, z: 30 };
const point: Point = p;
console.log(point.x, point.y);
This code shows that an object with extra properties can still be assigned to a type if it has the required shape.
Execution Table
StepActionEvaluationResult
1Define interface Point with x:number and y:numberType Point createdPoint expects x and y as numbers
2Create object p with x=10, y=20, z=30Object p createdp has x,y,z properties
3Assign p to variable point of type PointCheck if p matches Point shapep has x and y, extra z ignored, assignment allowed
4Access point.x and point.yRetrieve valuesOutputs 10 20
💡 Assignment allowed because p has at least the properties required by Point
Variable Tracker
VariableStartAfter Step 2After Step 3Final
pundefined{x:10, y:20, z:30}{x:10, y:20, z:30}{x:10, y:20, z:30}
pointundefinedundefined{x:10, y:20}{x:10, y:20}
Key Moments - 2 Insights
Why can an object with extra properties be assigned to a type?
Because TypeScript uses structural typing, it only checks if the object has the required properties with correct types, ignoring extra ones. See execution_table step 3.
Does the object need to explicitly declare the interface to be assigned?
No, the object just needs to have the right shape. It does not need to say it implements the interface. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what properties does object p have after step 2?
AOnly z
BOnly x and y
Cx, y, and z
DNo properties yet
💡 Hint
Check execution_table row 2 under Result column
At which step does TypeScript check if the object matches the type shape?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
See execution_table row 3 Action and Evaluation columns
If object p lacked property y, what would happen at step 3?
AAssignment allowed anyway
BTypeScript error: missing property y
CExtra properties cause error
DNo error, but runtime crash
💡 Hint
Structural typing requires all required properties; see key_moments about shape matching
Concept Snapshot
Structural typing means TypeScript checks if an object has required properties and types.
Extra properties do not cause errors.
Objects do not need explicit interface declarations.
Assignment allowed if shape matches.
This enables flexible and safe code.
Full Transcript
Structural typing in TypeScript means that when you assign an object to a variable of a certain type, TypeScript checks if the object has the required properties with the correct types. It does not require the object to explicitly declare that it implements the type. For example, if you have an interface Point with properties x and y, and an object with x, y, and extra property z, TypeScript allows assigning that object to a Point variable because it has at least the required properties. This is shown in the execution steps where the object p with x=10, y=20, z=30 is assigned to point of type Point. The extra property z is ignored. This approach helps write flexible code while keeping type safety.