sindresorhus / is

Type check values

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Falsy values are treated as `Array` type

szmarczak opened this issue · comments

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`.

The same happens with false, 0, null, NaN or even an empty string.

I'd like to take a look at this.

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.

@sindresorhus Hi maintainers! I opened a PR to address this.