Intersection Types (&)

With intersection types different types can be blended into one unit, these kind of type will hold all properties belonging to intersected types.

Example: In the given below example we are combining Person and Employee Types.

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

interface Employee {
  employeeId: number;
  department: string;
}

type EmployeePerson = Person & Employee;

const pankaj: EmployeePerson = {
  name: "Pankaj Bind",
  age: 20,
  employeeId: 12345,
  department: "Engineering"
};

console.log(pankaj);

Output:

{
name: 'Pankaj Bind',
age: 20,
employeeId: 12345,
department: 'Engineering'
}

Type Manipulation in TypeScript

TypeScript offers strong tools for types manipulation and transformation, these tools allow programmers to create new types by composing, intersecting, unionizing, mapping and conditioning existing ones, in this article we will investigate some of the advanced type-manipulation features in TypeScript including:

Table of Content

  • Intersection Types (&)
  • Union Types (|)
  • Mapped Types
  • Conditional Types

Similar Reads

Intersection Types (&)

With intersection types different types can be blended into one unit, these kind of type will hold all properties belonging to intersected types....

Union Types (|)

When you use a union type, you can create a type to be among many types, this indicates that one can choose from any of the assigned types....

Mapped Types

When you make mapped types, you make new typed entities by changing what is similar in existing ones, this is helpful if you want to apply one transformation to each property in a type....

Conditional Types

Enabling conditional types helps you form types in line with a certain test, this is practical in terms of making types that rely on other types....

Conclusion

TypeScript has strong and flexible type manipulation capabilities, developers can make the code more secure and readable by combining types of intersections, unions, mapped and conditional types, for a complete utilization of the static type system strength in TypeScript, those features have to be employed....

Contact Us