-
Notifications
You must be signed in to change notification settings - Fork 733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default config DateTime constructor #1466
base: master
Are you sure you want to change the base?
Conversation
Set default value for config in DateTime constructor
|
The constructor is private API and should not be called by anyone but Luxon itself. |
We are using class-transformer. I think class-transformer uses the constructor to try to create a deep copy of the DateTime instance. |
I haven't used class-transformer myself. |
We do use transformations. In the class we are tranforming to, the date is not even stored as a luxon DateTime, but as string. Luxon is only used in the plain object we are trying to convert to the class. However, to my understanding, class-transformer will first try to create a deep copy of the object to be transformed before executing the transformations on the object (probably to avoid accidentally altering the original object in the transformations). This is where the constructor of DateTime is used an where the problem above pops up. This issue is related to this issue on the class-transformer repository: typestack/class-transformer#132 We are currently using this workaround in our codebase, but I would very much like to avoid this.
|
I cannot reproduce what you mentioned. As long as class transformer understands that you want a import 'reflect-metadata';
import {plainToClass, Type} from 'class-transformer';
import {DateTime} from 'luxon';
class Test {
@Type(() => String)
dateTime: string;
}
console.log(plainToClass(Test, { dateTime: DateTime.now() })); |
When using DateTime with mappers such as class-transfomers, these libraries will try to use the DateTime constructor without parameters. This currently will throw an exception. Setting a default value for config in the constructor solves this problem.