loading...

re: Advanced TypeScript Types cheat sheet (with examples) VIEW POST

TOP OF THREAD FULL DISCUSSION
re: A really well-written article with good examples. Good job! Would like to add one more thing I like to use and you might find it interesting: t...
 

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.
}

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 :))

 

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.

 

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.

Code of Conduct Report abuse