DEV Community

Discussion on: Advanced TypeScript Types cheat sheet (with examples)

ionellupu profile image
Ionel lupu

What are some really good real world examples for this? (3,4 examples).

Wouldn't the optional operator be enough?

function foo(bar?: string) {
  if (!bar) {
    // handle if bar is falsy
  // do stuff if value not falsy.
Enter fullscreen mode Exit fullscreen mode

The only thing I can think of is when you really need the parameter to be exactly null. But I never have this use case in the projects I work on, so I think something is fishy :))

khrisl33t profile image

Actually it's just personal preference to have null values instead of undefined.
The example I provided is actually not that great, because I tend to use this more with objects and their properties.

michaeljota profile image
Michael De Abreu

The difference between the two, is that in Maybe the value is required, and in the second one is optional. So, you can have Maybe values and those values would be either defined or not, but would still be required. I have found this to be useful in React.