I'm attempting to enhance the prototype of the HTMLElement object in my main.ts
file, with the intention of using it across my entire Angular 6 project.
However, I am encountering the error message:
Property 'fadeOut' does not exist on type 'HTMLElement'
.
HTMLElement.prototype.fadeOut = function(duration: number = 300): Promise<void> {
const keyframes: AnimationKeyFrame[] = [{ opacity: 1 }, { opacity: 0 }];
return new Promise(resolve => {
const animation: Animation = this.animate(keyframes, duration);
animation.onfinish = () => resolve();
});
};
const circle = document.getElementById('circle');
HTMLElement.prototype.fadeOut = function(duration = 300) {
const keyframes = [{ opacity: 1 }, { opacity: 0 }];
return this.animate(keyframes, duration).finished
};
circle.fadeOut(2000);
#circle {
height: 100px;
width: 100px;
margin: 50px;
border-radius: 50%;
background-color: #0f477f;
}
<div id="circle"></>
What could be causing this issue?
Where should I place this code to ensure that the method is accessible globally?
Is there a way to refactor this code for improved clarity and efficiency?