From 5565c5e3baed0f63bccf358933b5f54bf3b9cfc8 Mon Sep 17 00:00:00 2001 From: Martin Eneqvist Date: Fri, 6 Sep 2024 17:52:56 +0200 Subject: [PATCH] Fix passing in assertion message to `assertArray` (#210) Co-authored-by: Sindre Sorhus --- source/index.ts | 8 +++++--- test/test.ts | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/source/index.ts b/source/index.ts index 9fd1923..e7835cf 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1143,14 +1143,16 @@ export function assertAny(predicate: Predicate | Predicate[], ...values: unknown } } -export function assertArray(value: unknown, assertion?: (element: unknown) => asserts element is T, message?: string): asserts value is T[] { +export function assertArray(value: unknown, assertion?: (element: unknown, message?: string) => asserts element is T, message?: string): asserts value is T[] { if (!isArray(value)) { throw new TypeError(message ?? typeErrorMessage('Array', value)); } if (assertion) { - // eslint-disable-next-line unicorn/no-array-for-each, unicorn/no-array-callback-reference - value.forEach(assertion); + for (const element of value) { + // @ts-expect-error: "Assertions require every name in the call target to be declared with an explicit type annotation." + assertion(element, message); + } } } diff --git a/test/test.ts b/test/test.ts index 4017cc3..258fb89 100644 --- a/test/test.ts +++ b/test/test.ts @@ -773,6 +773,10 @@ test('is.array', t => { x[0]?.toFixed(0); } }); + + t.throws(() => { + assert.array([1, '2'], assert.number, 'Expected numbers'); + }, {message: /Expected numbers/}); }); test('is.function', t => {