In TypeScript, the duck-typing feature ensures type safety. As a result of the duck-typing rule, the TypeScript compiler verifies that two objects are identical in terms of having matching properties and methods. This technique is used to compare two objects by determining if they have the same type-matching properties and object members.
Example 1: In the below code we create three classes: pigeon, penguin, and owl. All three classes have a property called "sound". Penguin class has an additional method called swim. When we assign owl to penguin variable it gives no error as they both have the same properties. The same goes when the pigeon is assigned to the owl type variable or when the penguin object is assigned to the pigeon type variable. But we cannot assign the pigeon-type objects to the penguin as the penguin has an additional class method but the pigeon doesn't.
JavaScript
class Pigeon {
sound = "coos";
}
class Owl {
sound = "hoots";
}
class Penguin {
sound = "peeps";
swim() {
console.log("I'm a bird and i can swim");
}
}
let pigeon: Pigeon = new Owl(); // Works
let owl: Owl = new Pigeon(); // Works
let pigeon2: Pigeon = new Penguin(); // Works
let penguin: Penguin = new Pigeon(); // Compile time error
// Printing values
console.log("A pigeon " + pigeon.sound);
console.log("An owl " + owl.sound);
console.log("A pigeon " + pigeon2.sound);
console.log("A penguin " + penguin.sound);
Output: When we compile the .ts file we get this error:
error TS2741: Property 'swim' is missing in type 'Pigeon' but required in type 'Penguin'. let penguin: Penguin = new Pigeon();
When we run the .js file using the command:
node filename.js
Output:
A pigeon hoots
An owl coos
A pigeon peeps
A penguin coos
Example 2: In this example, we create two classes ordinary_phone and iPhone. This example is just for the sake of having a better understanding. Both normal phones and iPhones help users call and text, in the example iPhone has a method camera_experience(). So iPhone consists of all the functions of an ordinary phone and it has a good camera. So, if we create a variable of type iPhone and pass ordinary_phone object it will work, but the opposite isn't possible as ordinary_phone doesn't consist of camera_experience() method.
JavaScript
class ordinary_phone {
functions = ["calls", "messages"];
}
class iphone {
functions = ["calls", "messages"];
camera_experience() {
console.log("i am very well known for my camera");
}
}
let phone1: ordinary_phone = new iphone();
console.log(phone1.functions);
console.log(phone1.camera_experience());
let phone2: iphone = new ordinary_phone();
Output: After compiling the .ts file we get this error:
error TS2339: Property 'camera_experience' does not exist on type 'ordinary_phone'. console.log(phone1.camera_experience()); ~~~~~~~~~~~~~~~~~ one.ts:427:5 - error TS2741: Property 'camera_experience' is missing in type 'ordinary_phone' but required in type 'iphone'. let phone2: iphone = new ordinary_phone(); ~~~~~~ one.ts:419:3 camera_experience() { ~~~~~~~~~~~~~~~~~ 'camera_experience' is declared here.
When we run the .js file using the command:
node filename.js
Output:
[ 'calls', 'messages' ]
i am very well known for my camera
undefined
Similar Reads
TypeScript Structural Typing TypeScript type system is based on structural typing, therefore, TypeScriptâs type compatibility is determined by the structure (i.e., the properties and methods) rather than explicit declarations or names. In this article, we will learn in detail about TypeScript Structural Typing.What is Structura
5 min read
Nominal Typing in TypeScript TypeScript is a language that has a static type system and enhances JavaScript by adding descriptions of types. One key part of TypeScriptâs type system is its support for structural typing. But there are times when developers need more control over type identity, this is where the idea of nominal t
6 min read
TypeScript Union The TypeScript union has the ability to combine one or two different types of data (i.e., number, string, float, double, etc). It is the most powerful way to express a variable with multiple types. Use pipe ('|') symbol to combine two or more data types to achieve Union type. Syntax: (type1|type2|ty
3 min read
TypeScript Writing Good Overloads In this article, we are going to learn about Writing Good Overloads in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. For writing good overloads, you should always prefer parameters with union types instead of overloads when possible beca
3 min read
TypeScript Tutorial TypeScript is a superset of JavaScript that adds extra features like static typing, interfaces, enums, and more. Essentially, TypeScript is JavaScript with additional syntax for defining types, making it a powerful tool for building scalable and maintainable applications.Static typing allows you to
8 min read