Good day!
After upgrading a v8 project to v9, I encountered some errors related to extensions in the compiler. These errors are not present in another project that I also recently upgraded.
The extensions, which are basic for constructors and prototypes, are set up correctly. They work without any issues in other projects, so it's puzzling why they are causing errors in this specific project.
To confirm the basic usage:
Add the following to typings.d.ts
:
interface StringConstructor {
isNullOrEmpty(str: string): boolean;
}
interface String {
padStartWithChar(char: string, totalSize: number): string;
}
Implementation:
export {};
String.isNullOrEmpty = function(str: string): boolean {
// ...
};
String.prototype.padStartWithChar = function(this: string, char: string, totalSize: number): string {
// ...
}
And import in main.ts
:
// ...
import './extensions/string-extensions';
// ...
The paths are correct and the files are in the expected locations, yet build errors persist. The compilation fails with errors related to the extensions, causing the build to halt.
...
src/extensions/string-extensions.ts:9:8 - error TS2339: Property 'isNullOrEmpty' does not exist on type 'StringConstructor'.
9 String.isNullOrEmpty = function(str: string): boolean {
...
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Browsing results in a 'Cannot GET/' message, but saving files triggers a re-build that eventually completes successfully despite the error messages.
If anyone has insight on solving this issue, I would greatly appreciate the help. I have compared configurations between the two projects but have not been able to identify the cause of the errors.
Thank you.
Edit:
After reverting and attempting the upgrade again, I encountered errors with the extensions at the initial steps of the upgrade process. Skipping certain steps and attempting a direct upgrade to v9 also resulted in issues.
Update:
Following Richard's advice, most of the extension errors were resolved. However, errors related to Date extensions persist, particularly with the DateConstructor
interface and the usage of moment
.
Update edit:
Date extensions exhibit similar issues to other extensions, but the presence of import moment from 'moment'
seems to complicate matters. Removing this import does not resolve the problem, indicating that Date extensions behave differently compared to String or Array extensions.