Some time ago I created two simple file to extend functionalities for some types. I created a global.d.ts
in this way:
export { }
declare global {
interface Date {
isLeapYear() : boolean;
}
interface Array<T> {
contains(item: T, compareFunc?: (value: T) => boolean): boolean;
sum(callbackfn: (value: T) => number) : number;
}
interface Element {
closest(selector: string) : Element;
}
interface String {
contains(text: string) : boolean;
}
}
Then I created file extensions.ts
whit implementations on the prototype. I imported it in my polyfills.ts
:
import "zone.js/dist/zone";
import "./extensions";
In this way I'm able to use these methods everywhere in my code. Now I need to do the same thing in an Angular 11 library. I created global.d.ts
and extensions.ts
, but now? There isn't a polyfills.ts
file and if I try with a simple import where I need one of this methods (import "../../extensions";
) nothing works. In the console I obtain this errors:
projects/test_library/src/components/test.component/test.component.ts:428:48 - error TS2339: Property 'isLeapYear' does not exist on type 'Date'.
428 return new Date(value).isLeapYear();
~~~~~~~~~~~~~~~~~
projects/test_library/extensions.ts:2:20 - error TS2339: Property 'isLeapYear' does not exist on type 'Date'.
2 Date.prototype.isLeapYear= function () : string {
How can I import globally my extensions? And why I get TS2339 error in extensions.ts
when in other projects it works fine?
polyfills.ts
of the consumer application? – Apodictic