Having difficulty with TypeScript/JavaScript
Currently working with an array of functions like this
private listeners: ((name: string) => void)[] = [];
Successfully adding functions to the array within another function.
Now looking to trigger those functions on button press
button.onclick = function(){
this.listeners.forEach((callback) => callback(username));
}
When attempting to execute this code, nothing happens. It appears to be due to lack of recognition of listeners
.
Next attempt was
button.onclick = function(listeners){
listeners.forEach((callback) => callback(username));
}
Now encountering tsc error
error TS2339: Property 'forEach' does not exist on type 'MouseEvent'.
Suspecting a missing type declaration, but unsure how to specify that this array consists of functions of type
((name: string) => void)[] = [];
If anyone has a quick workaround, it would be greatly appreciated.
And just to address any potential questions: I can confirm that the add function did increase the size of the listeners
array at least (it's reassuring - :D).