Table of contents
1.
Duck Typing TypeScript
2.
Example
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

Duck Typing TypeScript

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Duck Typing TypeScript

Duck-Typing is a method/rule used by TypeScript to check type compatibility for more complex variable types. This method is used to compare two objects by determining whether they have the same type of matching names or not. It means we can't change a variable's signature. 


Assume we assign an object with two attributes, such as name and address, and the following time we set an object with more or fewer properties, or both properties are not set (name, address). The TypeScript compiler will output a compile-time error in that situation. Duck typing is the name for this notion. 


In TypeScript programming, the duck-typing feature ensures type safety. The TypeScript compiler uses the duck-typing rule to determine whether an object is identical.

Also see, Types of information system

Example

The following example illustrates the duck typing in TypeScript:

class Dog {  
    sound = "barking";  
}  
class Lion {  
    sound = "roaring";  
}  
class Goat {  
    sound = "bleat";  
    swim(){  
        console.log("Cannot Swim!");  
    }  
}  
let lion: Lion = new Dog(); // substitutes  
let dog: Dog = new Lion(); // substitutes  
let lionTwo: Lion = new Goat();  
//let goat: Goat = new Lion(); // IDE & compiler error  
console.log("Lion Sound: "+lion.sound);  
console.log("Dog sound: "+dog.sound);  
console.log("Lion sound: "+lionTwo.sound);  

 

Because the Goat class includes an additional function, it is impossible to substitute a Lion for a Goat in the example above (so Lion fails duck typing). In duck typing, dog and lion are interchangeable because a lion can't accomplish anything that a dog can't, and vice versa.

FAQs

  1. What is the principle of duck typing?
    The duck test, "If it walks like a duck and quacks like a duck, then it must be a duck" is used in computer programming to verify whether an object may be utilized for a specific purpose.
     
  2. Why is duck typing useful?
    Duck typing is a concept in dynamic typing in which the type or class of an object is less important than the methods it specifies. When you use duck typing, you don't verify types at all. Instead, you're looking for a specific function or trait to see if it exists.
     
  3. What is duck-typing in TypeScript?
    Duck-Typing is a method/rule used by TypeScript to check type compatibility for more complex variable types. The duck-typing method in TypeScript is used to compare two objects by determining whether they have the same type matching names or not. Duck typing is the name for this notion.

 

Key Takeaways

Duck-Typing is a method/rule used by TypeScript to check type compatibility for more complex variable types.

Hey there!! Did you enjoy our blog? For more such exciting blogs on TypeScript head to the following links:

TypeScript - Switch case statement 

TypeScript Map

Happy reading!

Live masterclass