By 06/07/2015 18 Comments

Duck-Typing In TypeScript

Duck-Typing:

  • Duck-Typing is a method/rule for checking the type compatibility for more complex variable types.
  • TypeScript compiler uses the duck-typing method to compare one object with other object by comparing that the both objects have the same type matching properties/variables names or not. If both objects are different from one another and have different property names then the TypeScript compiler will generates the compile-time error through the duck-typing method/rule.
  • Duck-typing feature gives type safety in TypeScript code.
  • Through the duck-typing rule TypeScript compiler checks that an object is same as other object or not.
  • According to duck-typing method, the both objects must have matching same properties/variables types.
  • Duck-typing is a powerful feature which brings strong typing concepts in TypeScript code.

Example:

// Following is an object creation with different properties and it is assigned to a local variable bookList
var bookList = { book: "C#", id: 1 };

/* In following statements re-assigning another object with same properties to bookList variable. Notice each new object declaration has same properties as well as also a new property. This is possible through duck-typing rule. */

bookList = { book: "TypeScript", id: 2, price: 1000 };
bookList = { book: "Asp.net", id: 3, price: 2000, isbn: 2054 };

document.write("Book: " + bookList.book + " <br/>");
document.write("Id: " + bookList.id);





Next Tutorial →


← Previous Tutorial


Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .