Referencing the issue at https://github.com/moment/luxon/issues/260, I am looking to extend the DateTime object as shown below:
import { DateTime } from 'luxon';
function fromUnix(tsp?: number): DateTime {
return DateTime.fromMillis(tsp * 1000);
}
function toUnix(): number {
const seconds = (this as DateTime).toSeconds();
return parseInt(String(seconds));
}
if (!DateTime.prototype.fromUnix) {
DateTime.prototype.fromUnix = fromUnix;
}
if (!DateTime.prototype.toUnix) {
DateTime.prototype.toUnix = toUnix;
}
However, I am unsure how to add type definitions for these methods in order to have TypeScript validate them.
I have attempted the following approach:
declare module 'luxon/src/datetime' {
interface DateTime {
fromUnix(tsp?: number): DateTime;
toUnix(): number;
}
}
But this results in an error stating
'DateTime' only refers to a type, but is being used as a value here.
when I try to use DateTime
like so:
import { DateTime } from 'luxon';
class MyClass {
start = DateTime.now();
}
If anyone could provide guidance on resolving this issue, it would be greatly appreciated. Thank you for your assistance.