From 23ef246895290e8dd2db5bcaea1d096e3b6d813b Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Mon, 21 Feb 2022 18:36:36 +0100 Subject: [PATCH 1/2] Fix ow.number message when NaN is encountered --- source/predicates/predicate.ts | 2 +- test/number.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/source/predicates/predicate.ts b/source/predicates/predicate.ts index f90bfc3..b456698 100644 --- a/source/predicates/predicate.ts +++ b/source/predicates/predicate.ts @@ -87,7 +87,7 @@ export class Predicate implements BasePredicate { const label_ = label?.slice(this.type.length + 1); // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${is(value)}\``; + return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${Number.isNaN(value) ? 'NaN' : is(value)}\``; }, // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call validator: value => (is as any)[typeString](value), diff --git a/test/number.ts b/test/number.ts index a33f117..5660351 100644 --- a/test/number.ts +++ b/test/number.ts @@ -10,6 +10,10 @@ test('number', t => { ow('12' as any, ow.number); }, {message: 'Expected argument to be of type `number` but received type `string`'}); + t.throws(() => { + ow(Number.NaN as any, ow.number); + }, {message: 'Expected argument to be of type `number` but received type `NaN`'}); + t.throws(() => { ow('12' as any, 'foo', ow.number); }, {message: 'Expected `foo` to be of type `number` but received type `string`'}); From 8abbcdadd2f08e686a822f8f00b3d6620c083958 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 22 Feb 2022 00:41:09 +0700 Subject: [PATCH 2/2] Update predicate.ts --- source/predicates/predicate.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/source/predicates/predicate.ts b/source/predicates/predicate.ts index b456698..b862b8a 100644 --- a/source/predicates/predicate.ts +++ b/source/predicates/predicate.ts @@ -86,6 +86,7 @@ export class Predicate implements BasePredicate { // We do not include type in this label as we do for other messages, because it would be redundant. const label_ = label?.slice(this.type.length + 1); + // TODO: The NaN check can be removed when `@sindresorhus/is` is fixed: https://github.com/sindresorhus/ow/issues/231#issuecomment-1047100612 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${Number.isNaN(value) ? 'NaN' : is(value)}\``; },