Categories
JavaScript Answers

How to combine members of multiple types in a TypeScript annotation?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.