TIL: Typescript Composite Types

The TIL series is a series of short blogposts, aimed to document small things learnt everyday

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



Educator, Founder @ Interleap

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store