Typescript extension: TypeError: intermediate value is not a function

Viewed 16

I've created an extension on date class like below:

import {addMinutes, format as dtFormat} from "date-fns";
import {DateFormat} from "core/constants";

export function formatUTC(date, format) {
    return dtFormat(addMinutes(date, date.getTimezoneOffset()), format);
}

declare global {
    interface Date {
        toDbDateTime(): string;

        toDbDateOnly(): string;
    }
}

Date.prototype.toDbDateTime = function (): string {
    return formatUTC(this, DateFormat.DATETIME);
};

Date.prototype.toDbDateOnly = function (): string {
    return formatUTC(this, DateFormat.DATE);
};

But calling the extension

console.log(new Date().toDbDateTime())

I'm getting error:

TypeError: (intermediate value).toDbDateTime is not a function
    at TransactionsService.createTransaction (/Users/firefly/IdeaProjects/qms-api/dist/transactions/transactions.service.js:40:32)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
0 Answers
Related