Sometimes, we want to combine members of multiple types in a TypeScript annotation.
In this article, we’ll look at how to combine members of multiple types in a TypeScript annotation.
How to combine members of multiple types in a TypeScript annotation?
To combine members of multiple types in a TypeScript annotation, we can create intersection types.
For instance, we write
interface ClientRequest {
userId: number;
sessionKey: string;
}
interface Coords {
lat: number;
long: number;
}
type Combined = ClientRequest & Coords;
to combine the properties of the ClientRequest and Coords into one type with ClientRequest & Coords.
Then Combined would have the properties of both interfaces in a single type.
Conclusion
To combine members of multiple types in a TypeScript annotation, we can create intersection types.