I have written a custom extension in my extensions/date.ts
file which adds a method to the Date interface:
interface Date {
addDays: (days: number) => Date
}
Date.prototype.addDays = function(days: number): Date {
if (!days) return this;
let date = this;
date.setDate(date.getDate() + days);
return date;
};
The extension is imported at the beginning of the server.ts
file:
import './extensions/date';
import * as dotenv from 'dotenv';
dotenv.config()
...rest imports
The extension works correctly and the app compiles successfully. However, sometimes after making changes and having the app automatically recompiled using nodemon
and run with node-ts
, the app breaks and TypeScript no longer recognizes Date.addDays()
as a valid function.
To temporarily solve this issue, I used to move the import statement higher up in the file, but now I have reached a limit.
The error message is the familiar 2339:
Property 'addDays' does not exist on type 'Date'. (2339)
Has anyone encountered this problem before? Any suggestions for a workaround?