My development setup includes a service called DomService
for all DOM manipulation tasks.
Additionally, I have another service called ModalService
that handles modal functionality.
Within the ModalService
, there are some events being bound, with a method from DomService
utilized as the listener. Here's an example:
document.body.addEventListener('focus', this.domService.keyPress.bind(this), true);
The keyPress function within DomService
looks somewhat like this:
keyPress(event){
if (event.which === key.escape) {
event.preventDefault();
this.hide();
}
}
Although it functions correctly, TypeScript seems to see references to this
as pointing to the DomService
class instead of the ModalService
class. As a result, TypeScript is flagging errors about the hide
property not existing on type DomService
.
Is there any way to resolve this issue and make TypeScript understand the context better?