Skip to content

Cannot transform string to number #1672

Open
@stad-nico

Description

@stad-nico

Description

Trying to convert number strings wont cast the type to number. This was supposed to be fixed in 0.2.2 (Issue #179)

Minimal code-snippet showcasing the problem

import { plainToClass } from "class-transformer";
import { IsDate } from "class-validator";
import "reflect-metadata";

export class T {
	@IsDate()
	number!: number;
}

const m = plainToClass(T, { number: "10" }, { enableImplicitConversion: true });
console.log(m); // -> T { number: "10" }

Expected behavior

console.log(m); // -> T { number: 10 }

Actual behavior

console.log(m); // -> T { number: "10" }

The workaround using @Type(() => Number) still works, however I expect number strings to be converted without additionally specifying the type.

Using

"dependencies": {
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "reflect-metadata": "^0.2.1",
    "ts-node": "^10.9.2",
    "typescript": "^5.3.3"
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions