non-nullish throw syntax: `throw?`

throw? maybeError would be more convenient than if (maybeError) throw maybeError.

Sometimes, if you have an error returning function, you want to throw it (if you decide not to use other logic for the return value):

if (validate(foo)) throw validate(foo)

where validate returns an error if invalid, otherwise null. This is duplicating the amount of work though, and not as DRY.

So next you:

const err = validate(foo)
if (err) throw err

But with nullish throw you could shorten this to:

const err = validate(foo)
throw? err

or just

throw? validate(foo)
1 Like