ts-reset icon indicating copy to clipboard operation
ts-reset copied to clipboard

Feature request - Smarter `Boolean`

Open Calvein opened this issue 2 years ago • 0 comments

This is a great project 😍

I add a suggestion to "patch" Boolean because I have been stumped by it at multiple occasions, e.g:

const fn = (arg: 'string' | null) => {
  const bar = Boolean(arg)
  if (bar) {
      return arg.toUpperCase()
          // ^? (parameter) arg: "string" | null
  }

  const baz = !!arg
  if (baz) {
      return arg.toUpperCase()
          // ^? (parameter) arg: "string"
  }

  return arg
}

And the TS Playground link.


In real code examples, I usually have a combination of variables (e.g: const hasData = Boolean(!isLoading && data && !hasError)) and I found Boolean to be quite "dumb" compared to the less elegant !!. I don't know if it's normal and I'm missing something of if it could be patch like Array#filter.

Calvein avatar Feb 21 '23 14:02 Calvein