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 Generic Object Types TypeScript Generic Object Types allow you to create flexible and reusable type definitions for objects. These generic types can work with different shapes of objects while providing type safety, ensuring your code is both robust and adaptable. They are particularly useful for creating functions or c
3 min read
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
TypeScript in operator narrowing Type In this article, we will learn about the 'in' operator narrowing Type in Typescript. In TypeScript, the 'in' operator is used to narrow or refine the type of an object within a conditional statement or block. It checks whether a specific property or key exists within an object, and if it does, it na
3 min read
How to Check the Type of an Object in Typescript ? When working with TypeScript, understanding how to check the type of an object is crucial for ensuring type safety and maintaining code integrity. TypeScript, being a statically typed superset of JavaScript, provides several approaches to accomplish this task as listed below.Table of ContentUsing th
3 min read
TypeScript Interface vs Type Statements In TypeScript, interface and type are used to define the structure of objects and custom types. While interfaces enforce contracts for classes and support multiple inheritance, types offer flexibility for defining complex and reusable type aliases. Each serves distinct use cases.Table of ContentInte
3 min read