0
0
Typescriptprogramming~10 mins

How structural typing differs from nominal typing in Typescript - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How structural typing differs from nominal typing
Start: Define Type A
Define Type B
Compare Types
Nominal Typing: Compare names
Structural Typing: Compare shape
Assign or Reject
End
This flow shows how nominal typing compares type names, while structural typing compares the shape (properties) of types to decide compatibility.
Execution Sample
Typescript
type Cat = { name: string };
type Dog = { name: string };

let cat: Cat = { name: "Whiskers" };
let dog: Dog = cat; // Allowed in structural typing
Assigning a Cat object to a Dog variable works because both have the same shape in structural typing.
Execution Table
StepActionType AType BComparisonResult
1Define Type Cat{ name: string }Type Cat created
2Define Type Dog{ name: string }Type Dog created
3Create cat objectcat = { name: "Whiskers" }cat assigned
4Assign cat to dogCatDogCompare names (Nominal): Cat != DogNominal: Error
5Assign cat to dogCatDogCompare shape (Structural): same propertiesStructural: Allowed
6EndAssignment allowed in structural typing
💡 Nominal typing rejects assignment due to different names; structural typing allows it due to matching shape.
Variable Tracker
VariableStartAfter AssignmentFinal
catundefined{ name: "Whiskers" }{ name: "Whiskers" }
dogundefinedError in nominal typing{ name: "Whiskers" } in structural typing
Key Moments - 2 Insights
Why does assigning 'cat' to 'dog' cause an error in nominal typing but not in structural typing?
Nominal typing checks if the type names match exactly (Cat vs Dog), so it rejects the assignment (see execution_table step 4). Structural typing checks if the properties match, ignoring names, so it allows it (step 5).
What does 'shape' mean in structural typing?
Shape means the properties and their types inside the object. If two types have the same properties with the same types, they have the same shape (execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does nominal typing reject the assignment?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Check the 'Result' column for nominal typing rejection.
According to the variable tracker, what is the value of 'dog' after assignment in structural typing?
Aundefined
BError
C{ name: "Whiskers" }
Dnull
💡 Hint
Look at the 'Final' column for 'dog' in variable_tracker.
If Type Dog had an extra property, how would structural typing handle assigning 'cat' to 'dog'?
AAllow assignment because names differ
BReject assignment because shapes differ
CAllow assignment ignoring extra properties
DError due to missing name property
💡 Hint
Structural typing requires matching properties; extra properties cause mismatch.
Concept Snapshot
Nominal typing compares type names exactly.
Structural typing compares object shapes (properties).
In TypeScript, structural typing allows assignment if shapes match.
Nominal typing rejects if names differ, even if shapes match.
Structural typing is more flexible and common in TypeScript.
Full Transcript
This visual execution shows how nominal and structural typing differ in TypeScript. We define two types, Cat and Dog, both with a name property. When assigning a Cat object to a Dog variable, nominal typing checks the type names and rejects the assignment because Cat and Dog are different names. Structural typing compares the properties inside the types and allows the assignment because both have the same shape: a name string. The variable tracker shows that 'cat' holds the object with name 'Whiskers', and 'dog' gets the same object in structural typing but causes an error in nominal typing. Key moments clarify why nominal typing rejects based on names and what shape means in structural typing. The quiz tests understanding of these steps and concepts.