phaser3-docs icon indicating copy to clipboard operation
phaser3-docs copied to clipboard

Typescript Bug: Property 'update' in type is not assignalbe to the same property in base type 'Image'

Open B3L7 opened this issue 7 years ago • 3 comments

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.

B3L7 avatar May 25 '18 12:05 B3L7

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.

ampled avatar May 29 '18 10:05 ampled

I've submitted a pull request for this here https://github.com/photonstorm/phaser/pull/3872

ampled avatar Jul 29 '18 11:07 ampled

The pull-request was accepted. This will be fixed when typedefs for Phaser 3.12 is published

ampled avatar Aug 02 '18 13:08 ampled