tiny-invariant throws on an empty string
jasikpark opened this issue · comments
I use https://github.com/fram-x/assert-ts#check-condition for my simple code assertions, which allows me to narrow string | null | undefined
to string
w/o throwing an error on an empty string. tiny-invariant differs in that - is that intentional?
I find it confusing, since ""
is still a string, but it does more clearly follow the if(!condition)
pattern more directly, though you can achieve the same w/ assert-ts
with assert(!!possiblyNullishOrEmptyString)
to cause it to throw on an empty string
tiny-invariant is explicitly documenting as checking whether the value passed as condition is falsy.
assert-ts has 2 signatures:
- when passing a boolean, it checks for false
- when passing anything else, it checks if the value is nullish
A check for nullish or a check for falsy will not be equivalent.
thx for the response - that's reasonable! i'll probably keep using ts-assert then 👍
From the description of this library:
An invariant function takes a value, and if the value is falsy then the invariant function will throw. If the value is truthy, then the function will not throw.
""
is falsy, and so will result in a throw
.
If you don't want to throw on ""
you could make your condition
more explicit
invariant(typeof value === 'string' && value !== '');