@Type(() => Date) breaks custom @Transform for classToPlain
Author: ehaynes99Created Sep 3, 2021Updated Mar 31, 2026
Labelstype: fixstatus: needs triage
Description
When using @Type(() => Date) along with an @Transform, calling plainToClass appears to first run the custom transform, then convert that result back into a Date.
class Example {
@Type(() => Date)
@Transform(() => '2222-12-31', { toPlainOnly: true })
timestamp!: Date;
}
const instance = plainToClass(Example, { timestamp: '2021-01-01' });
const t1 = instance.timestamp;
const t2 = classToPlain(instance).timestamp;
console.log('instance', t1, typeof t1, 'is date:', t1 instanceof Date);
console.log('plain obj', t2, typeof t2, 'is date:', t2 instanceof Date);Expected behavior
instance 2021-01-01T00:00:00.000Z object is date: true
plain obj 2222-12-31 string is date: falseActual behavior
instance 2021-01-01T00:00:00.000Z object is date: true
plain obj 2222-12-31T00:00:00.000Z object is date: trueThis seems to be specific to Date, as doing similar with @Type(() => Number) does not exhibit this behavior.
Source: typestack/class-transformer