Duck Typing

If it walks like a duck and it quacks like a duck, then it must be a duck.

Duck Typing is a programming concept used to determine the type of an object.

  • In nominative typing, an object is of a certain type based on the type it is explicitly declared with or the type it is derived from.
  • In duck typing, an object is of a certain type if it has all methods and properties of that type.

For example,

class Triceratops {    hasTail = true;}class Pikachu {    hasTail = true;}// Create a triceratopsitem1 = new Triceratops();// Create a pikachuitem2 = new Pikachu();// Create an object with hasTail = trueitem3 = {     hasTail: true };

Using the duck typing principle, all the three items in the example are of the same type since they all have a single, mutual property hasTail.