From c5b5bee6f2a779c4156b30d3465a0b6ef42ed0f5 Mon Sep 17 00:00:00 2001 From: simonkundrik <117163790+simonkundrik@users.noreply.github.com> Date: Fri, 10 Jul 2026 18:03:41 +0300 Subject: [PATCH] fix: array sub-type option respects its value, not just its presence An array option configured with a falsy sub-type such as `{ key: 'foo', number: false }` still enabled that coercion, because the sub-type (`boolean`/`string`/`number`) was chosen by key existence rather than by value. As a result `{ number: false }` (and `{ number: undefined }`) coerced array values to numbers, turning non-numeric values into NaN. Only enable the sub-type coercion when its value is truthy. Fixes #415 --- lib/yargs-parser.ts | 6 +++++- test/yargs-parser.mjs | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index 48a2d0f2..105510ac 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -121,7 +121,11 @@ export class YargsParser { string: 'strings', number: 'numbers' } - return arrayFlagKeys[key] + // Only enable the sub-type coercion when its value is truthy, so an + // explicit falsy value such as `{ number: false }` is ignored (#415). + return typeof opt === 'object' && (opt as Record)[key] + ? arrayFlagKeys[key] + : undefined }).filter(Boolean).pop() // assign key to be coerced diff --git a/test/yargs-parser.mjs b/test/yargs-parser.mjs index 4c692375..fc420ba0 100644 --- a/test/yargs-parser.mjs +++ b/test/yargs-parser.mjs @@ -1867,6 +1867,13 @@ describe('yargs-parser', function () { result.should.have.property('x').that.is.an('array').and.to.deep.equal([5, 2]) }) + it('should not coerce array values as numbers when `number` is false (#415)', function () { + const result = parser(['--foo', 'dog', 'cat'], { + array: [{ key: 'foo', number: false }] + }) + result.should.have.property('foo').that.deep.equals(['dog', 'cat']) + }) + it('should respect the type `string` option for arrays', function () { const result = parser(['-x=5', '2'], { configuration: {