DEV Community

Discussion on: Should I use fp-ts Option?

Collapse
 
tamcarre profile image
Tam CARRE
function getModifiedName (name: string): string | undefined {
  return isAuthor (name) ? lowercase (name) : lastName (name)
}

function greet (name?: string): string {
  const modifiedName = name ? getModifiedName (name) : undefined
  return modifiedName ? `Hello ${modifiedName}` : 'Greetings!'
}
Enter fullscreen mode Exit fullscreen mode

Shorter and more straightforward than both your traditional implementation, and your Option implementation.

Collapse
 
anthonyjoeseph profile image
Anthony G • Edited

I really like this! Maybe I should add this as an addendum to the nested ternary implementation - dividing functions can be a great way to increase readability.

Anyway, this inspired me to write a similarly terse fp-ts version - playground

Collapse
 
tamcarre profile image
Tam CARRE

I also really like this new fp-ts version.