diff --git a/src/TransformOperationExecutor.ts b/src/TransformOperationExecutor.ts index fe1da8e32..a294bf848 100644 --- a/src/TransformOperationExecutor.ts +++ b/src/TransformOperationExecutor.ts @@ -107,6 +107,9 @@ export class TransformOperationExecutor { return Number(value); } else if (targetType === Boolean && !isMap) { if (value === null || value === undefined) return value; + if (typeof value === 'string' && !isNaN(parseFloat(value))) return Boolean(+value); + if (typeof value === 'string' && ['true', 'false'].includes(value.toLowerCase())) + return value.toLowerCase() === 'true'; return Boolean(value); } else if ((targetType === Date || value instanceof Date) && !isMap) { if (value instanceof Date) { diff --git a/test/functional/implicit-type-declarations.spec.ts b/test/functional/implicit-type-declarations.spec.ts index 889565512..e4274395a 100644 --- a/test/functional/implicit-type-declarations.spec.ts +++ b/test/functional/implicit-type-declarations.spec.ts @@ -102,6 +102,24 @@ describe('plainToInstance transforms built-in primitive types properly', () => { @Type() boolean2: boolean; + + @Type() + boolean3: boolean; + + @Type() + boolean4: boolean; + + @Type() + boolean5: boolean; + + @Type() + boolean6: boolean; + + @Type() + boolean7: boolean; + + @Type() + boolean8: boolean; } const result: Example = plainToInstance( @@ -114,6 +132,12 @@ describe('plainToInstance transforms built-in primitive types properly', () => { number2: 100, boolean: 1, boolean2: 0, + boolean3: 'true', + boolean4: 'false', + boolean5: '1', + boolean6: '0', + boolean7: '', + boolean8: 'yes', }, { enableImplicitConversion: true } ); @@ -140,5 +164,11 @@ describe('plainToInstance transforms built-in primitive types properly', () => { it('should recognize and convert to boolean', () => { expect(result.boolean).toBeTruthy(); expect(result.boolean2).toBeFalsy(); + expect(result.boolean3).toBeTruthy(); + expect(result.boolean4).toBeFalsy(); + expect(result.boolean5).toBeTruthy(); + expect(result.boolean6).toBeFalsy(); + expect(result.boolean7).toBeFalsy(); + expect(result.boolean8).toBeTruthy(); }); });