Typescript Composite Types

Dec 30, 2022


Let’s say you have an object in Typescript that you know can be either of type Transaction or of type Order.

For such cases, you can do Type Union in the following manner —

const process = (input: Transaction | Order) => 

On the other hand, let’s say we have a scenario, where we will get an object that will have the type Order but will also have an additional field status. In that case, we can use type intersection —

const orderWithStatus: Order & {status: string}

You can read more here — https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html