DEV Community

Discussion on: What's the Difference Between Nominal, Structural, and Duck Typing?

Collapse
 
avantar profile image
Krzysztof Szala • Edited

In structural typing case, the code example returns false not because of difference of type. It returns false, because you’re trying to compare two different objects. Try to to create two instances of Dog class, and then compare them to each other. You will still get false.

When you use typeof dog == typeof person you will get true.

Collapse
 
awwsmm profile image
Andrew (he/him)

Good point! Thanks for clarifying.

Collapse
 
jzombie profile image
jzombie

In JS / TS,

if (dog instanceof Dog) {
 // true
}

if (dog instanceof Person) {
 // false
}
Enter fullscreen mode Exit fullscreen mode

should let you know if dog actually inherits the relevant class, and it still works even if you created extension classes for dogs, as each extension will still be a dog.