-
Hi! We have utility methods to encapsulate commonly used patterns with Luxon throughout our codebase (for example, making an However, I can see that the static and instance methods do not respect subclassing and still return instances of Luxon's classes. Is there a specific extension facility to do this with? I was hoping I didn't have to end up modifying the prototype. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Yeah, Luxon's methods don't know about your subclasses, so without some sort of dependency injection system, those methods aren't going to be able to create instances of those subclasses. IMO, modifying the prototype is the way to go. The risk is that one day Luxon adds a method with the same name and your code breaks, but that seems relatively unlikely, and if you are concerned about that, you could do some name prefixing. |
Beta Was this translation helpful? Give feedback.
-
Just in case anyone ends up here trying to find out how to do this... The answer by Yayo Arellano works well :) import {DateTime} from 'luxon';
declare module 'luxon/src/datetime' {
export interface DateTime {
plusMillis(millis: number): DateTime;
}
}
DateTime.prototype.plusMillis = function(millis: number): DateTime {
const _self = this as DateTime;
return _self.plus({milliseconds: millis});
}; |
Beta Was this translation helpful? Give feedback.
Yeah, Luxon's methods don't know about your subclasses, so without some sort of dependency injection system, those methods aren't going to be able to create instances of those subclasses.
IMO, modifying the prototype is the way to go. The risk is that one day Luxon adds a method with the same name and your code breaks, but that seems relatively unlikely, and if you are concerned about that, you could do some name prefixing.