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: {