Falsy values are treated as `Array` type
szmarczak opened this issue · comments
Szymon Marczak commented
const is = require('@sindresorhus/is');
is.assert.any([is.urlInstance], undefined);
Actual:
TypeError: Expected value which is `predicate returns truthy for any value`, received value of type `Array`.
Expected:
TypeError: Expected value which is `predicate returns truthy for any value`, received value of type `undefined`.
Sindre Sorhus commented
// @scraggo
Szymon Marczak commented
The same happens with false
, 0
, null
, NaN
or even an empty string.
Dave Cohen commented
I'd like to take a look at this.
Dave Cohen commented
Started looking into it.
assert.any
(source) gathers (with the spread operator) the 2nd argument, values
. It then calls assertType
with that gathered array as the 3rd argument.
any: (predicate: Predicate | Predicate[], ...values: unknown[]): void | never =>
// values is an array and evaluated as such in `assertType`:
assertType(is.any(predicate, ...values), AssertionTypeDescription.any, values),
So, that's why the message always fails with Array
type. Given this, any failure (not just falsy values and not just predicate arrays) have the same incorrect message:
assert.any([is.string], 1);
assert.any(is.string, 1);
At the moment, I'm not sure how to address this, but I think that's the crux of the issue.
Dave Cohen commented
@sindresorhus Hi maintainers! I opened a PR to address this.