Issue
I'm doing a simple loop through an Array and calling a method on each element.
this.scene.manager.scenes.forEach((scene) => {
if(scene.refresh){
scene.refresh();
}
});
This code works, but it shows a compile error:
error TS2339: Property 'refresh' does not exist on type 'Scene'.
It's because one of array items is different from the others.
How can I prevent this compile error, since the condition if(scene.refresh){
is already handling it?
Solution
I think using a type assertion could be a better approach to inform TypeScript that the property does exist!
Something like this:
this.scene.manager.scenes.forEach((scene) => {
if ('refresh' in scene) {
(scene as any).refresh();
}
});
Answered By - Freeman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.