Fix passing in assertion message to assertArray (#210)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
This commit is contained in:
parent
ab85d9bca9
commit
5565c5e3ba
2 changed files with 9 additions and 3 deletions
|
|
@ -1143,14 +1143,16 @@ export function assertAny(predicate: Predicate | Predicate[], ...values: unknown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertArray<T = unknown>(value: unknown, assertion?: (element: unknown) => asserts element is T, message?: string): asserts value is T[] {
|
export function assertArray<T = unknown>(value: unknown, assertion?: (element: unknown, message?: string) => asserts element is T, message?: string): asserts value is T[] {
|
||||||
if (!isArray(value)) {
|
if (!isArray(value)) {
|
||||||
throw new TypeError(message ?? typeErrorMessage('Array', value));
|
throw new TypeError(message ?? typeErrorMessage('Array', value));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (assertion) {
|
if (assertion) {
|
||||||
// eslint-disable-next-line unicorn/no-array-for-each, unicorn/no-array-callback-reference
|
for (const element of value) {
|
||||||
value.forEach(assertion);
|
// @ts-expect-error: "Assertions require every name in the call target to be declared with an explicit type annotation."
|
||||||
|
assertion(element, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -773,6 +773,10 @@ test('is.array', t => {
|
||||||
x[0]?.toFixed(0);
|
x[0]?.toFixed(0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.array([1, '2'], assert.number, 'Expected numbers');
|
||||||
|
}, {message: /Expected numbers/});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.function', t => {
|
test('is.function', t => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue