Typescript Bug: Property 'update' in type is not assignalbe to the same property in base type 'Image'
I run in to this problem when extending an image or sprite. Typescript errors when I try to override the update method.
Sample class:
export default class Cloud extends Phaser.GameObjects.Image {
constructor(config) {
super(config.scene, config.x, config.y, 'objects', 'cloud');
config.scene.add.existing(this);
}
update(time, delta) {
this.x -= .125;
if (this.x <= -48) {
this.x = (this.scene as any).mapWidth;
}
}
}
Gives me this typescript error on the update method:
Property 'update' in type 'Cloud' is not assignable to the same property in base type 'Image'.
Type '(time: any, delta: any) => void' is not assignable to type '() => void'.
(method) Cloud.update(time: any, delta: any): void
To be overridden by custom GameObjects. Allow base objects to be used in a Pool.
If I take out the parameters from the update method the error goes away but I can foresee wanting to access time in the update method in the future.
Thanks.
While the typedefs represent the actual code in the base class GameObject, Typescript doesn't allow or represent javascript's feature of letting any function allow any amount of arguments without explicitly declaring it with rest parameters, like update(...args: any[]): void which would let any subclass of GameObject override the method signature.
This has to be solved by editing the jsdocs for GameObject#update with a proper @param-entry that translates to this signature with tsgen. Meanwhile, you can edit the Image#update signature in the typings-file directly with the rest parameters as a temporary fix.
I've submitted a pull request for this here https://github.com/photonstorm/phaser/pull/3872
The pull-request was accepted. This will be fixed when typedefs for Phaser 3.12 is published