TypeScript Object Intersection Types
Last Updated :
28 Apr, 2025
In TypeScript, Object Intersection Types are a way to create new types by combining multiple types into a single type. This is done using the & (ampersand) operator. Intersection types allow you to express that an object must have all the properties and methods of each type in the intersection. This is similar to taking the intersection of sets in mathematics.
Syntax
type TypeA = {
propA: number;
methodA: () => void;
};
type TypeB = {
propB: string;
methodB: () => void;
};
type CombinedType = TypeA & TypeB;
Parameters
- TypeA: This is an object type with a propA property (number) and a methodA method (a function).
- TypeB: This is another object type with a propB property (string) and a methodB method.
- CombinedType: This is an intersection type created by using TypeA & TypeB, which means an object of CombinedType must have all the properties and methods of both TypeA and TypeB.
Example 1: In this example, We define two object types, Dog and Bird, each with their own set of properties and methods. We create an intersection type HybridAnimal by combining Dog and Bird using the & operator. This means an object of the type HybridAnimal must have properties and methods from both a Dog and a Bird. We then create an object hybridPet of type HybridAnimal. It includes properties like name, breed, and wingspan, as well as methods like bark and fly. Finally, we access properties and call methods on hybridPet, demonstrating that it satisfies the requirements of both Dog and Bird types.
JavaScript
// Define two object types
type Dog = {
name: string;
breed: string;
bark: () => void;
};
type Bird = {
name: string;
wingspan: number;
fly: () => void;
};
// Create an intersection type
type HybridAnimal = Dog & Bird;
// Create an object of the intersection type
const hybridPet: HybridAnimal = {
name: "Griffin",
breed: "Labrador",
wingspan: 1.2,
bark: () => console.log("Woof!"),
fly: () => console.log("Flap, flap!"),
};
// Access properties and methods
console.log(hybridPet.name);
console.log(hybridPet.wingspan);
hybridPet.bark();
hybridPet.fly();
Output:
Griffin
1.2
Woof!
Flap, flap!
Example 2: In this example, We define two interfaces, Printable and Loggable, each with its own set of methods. We create an intersection type PrintableAndLoggable by combining Printable and Loggable using the & operator. This means an object of type PrintableAndLoggable must have both the print and log methods. We then create an object loggerAndPrinter of type PrintableAndLoggable. It includes both the print and log methods. Finally, we access and call the print and log methods on loggerAndPrinter, demonstrating that it satisfies the requirements of both Printable and Loggable interfaces.
JavaScript
// Define two interfaces
interface Printable {
print: () => void;
}
interface Loggable {
log: (message: string) => void;
}
// Create an intersection type
type PrintableAndLoggable =
Printable & Loggable;
// Create an object of the intersection type
const loggerAndPrinter: PrintableAndLoggable = {
print: () => console.log("Printing..."),
log: (message) => console.log(`Logging: ${message}`),
};
loggerAndPrinter.print();
loggerAndPrinter.log("Hello, GeeksforGeeks!");
Output:
Printing...
Logging: Hello, GeeksforGeeks!
Reference: https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types
Similar Reads
TypeScript Object Types TypeScript object types define the structure of objects by specifying property types, ensuring type safety and clarity when passing objects as function parameters.Optional properties, denoted with a ? provide flexibility for objects with varying properties. This approach enhances code robustness by
3 min read
Union Type to Intersection Type in TypeScript To Transform union type to intersection type we have different approaches. In this article, we are going to learn how to Transform union type to intersection type. Below are the approaches used to Transform union type to intersection type: Table of Content Using Distributive Conditional TypesUsing C
3 min read
What are intersection types in Typescript ? In Typescript, Although intersection and union types are similar, they are employed in completely different ways. An intersection type is a type that merges several kinds into one. This allows you to combine many types to create a single type with all of the properties that you require. An object of
3 min read
TypeScript Object Interfaces vs. Intersections TypeScript offers several features when it comes to defining complex data structures. In TypeScript, Object Interface and Intersections are two different features that serve different purposes when it comes to defining complex data structures. Object InterfacesObject Interfaces are used to define th
2 min read
TypeScript Interfaces Type TypeScript Interfaces Type offers an alternative method for defining an object's type, allowing for a distinct naming approach. Syntax:interface InterfaceName { property1: type1; property2?: type2; readonly property3: type3; // ... method1(): returnType1; method2(): returnType2; // ...}Parameters:in
2 min read
How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar
3 min read