Understanding TypeScript’s Structural Typing: Duck Typing and Compatibility Explained with Examples

Structural typing, commonly referred to as duck typing or static duck typing, is a type system used in TypeScript that emphasizes the shape or structure of types rather than their explicit declarations. This means that TypeScript evaluates the compatibility of types based on their member (property and method) structures rather than their names or declarations. Even if they were defined independently, two types are regarded as compatible if they share the same structure.

Of course, let’s dissect TypeScript’s structural type with examples, point by point:

1. Type Definitions:

interface Person {
name: string;
age: number;
}

interface Employee {
name: string;
age: number;
employeeId: string;
}
  •  Person and Employee are the two interfaces we define.
  • Name and age are attributes on both interfaces, but Employee also has employeeId.

2. Function Declaration:

function printPerson(person: Person) {
console.log(`Name: ${person.name}, Age: ${person.age}`);
}
  • We define the function printPerson, which takes a Person type parameter.
  •  The name and age of the object are required parameters for this function.

3. Object Creation:

const john: Person = { name: "John", age: 30 };
const sarah: Employee = { name: "Sarah", age: 25, employeeId: "E12345" };
  • John and Sarah are two items that we make.
  •  John is a Person of the Type with Name and Age.
  •  Sarah has the properties name, age, and employeeId of type Employee.

4. Function Calls:

printPerson(john);
printPerson(sarah);

We call the printPerson method with the inputs john and sarah.

Let’s now review each point:

1. Type Definitions:

  • We established two interfaces (Person and Employee) with various structural types.
  • When compared to Person, Employee has an additional property (employeeId).

2. Function Declaration:

  • We declared a function whose input is an object of the type Person.

3. Object Creation:

  • We created two objects, john and sarah, with distinct properties.

4. Function Calls:

  • printPerson(john);: We invoked the method using an object called Person, which is structurally compatible with the desired parameter, in the form of the name John.
  • printPerson(sarah);: Additionally, we used the Employee object sarah to call the procedure. This works because Sarah has the required name and age attributes, making it structurally compatible with Person. TypeScript uses structural typing to check compatibility.

In conclusion, TypeScript’s structural typing enables you to interact with objects based on their structure rather than merely their explicit type declarations. This encourages code reuse and adaptability when working with objects that have similar shapes, as shown in the example.

One thought on “Understanding TypeScript’s Structural Typing: Duck Typing and Compatibility Explained with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *