Closed jasikpark closed 1 year ago
tiny-invariant is explicitly documenting as checking whether the value passed as condition is falsy.
assert-ts has 2 signatures:
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 !== '');
I use https://github.com/fram-x/assert-ts#check-condition for my simple code assertions, which allows me to narrow
string | null | undefined
tostring
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 theif(!condition)
pattern more directly, though you can achieve the same w/assert-ts
withassert(!!possiblyNullishOrEmptyString)
to cause it to throw on an empty stringcodesandbox.io/s/assert-v-s-tiny-invariant-z2w4gq