Skip to content

[Types] Phaser.Scene classes do not declare init, preload or create methods #7241

@Bertie690

Description

@Bertie690

Version

  • Phaser Version: 3.90.0
  • Operating system: Every single one
  • Browser: All of the browser

Description

While phaser.d.ts does declare Phaser.Types.Scenes.SceneInitCallback, Phaser.Types.Scenes.ScenePreloadCallback and Phaser.Types.Scenes.SceneCreateCallback as the types of Scene.init, Scene.preload and Scene.create, said class does not declare any of these methods.
This results in poorer developer DX due to a lack of JSDoc hover info, as well as not appearing in the list of inherited methods from TypeScript (one cannot use override modifiers, for instance).

Example Test Code

class MyScene extends Phaser.Scene {
  // Produces type errors because Phaser.Scene doesn't define an `init` method to override
  public override init(): void {
    console.log("Hello from Phaser!");
  }
}

Additional Information

I have 2 potential ways to update the typings:

  1. Amend Phaser.Scene to contain properties with the correspondingly typed callbacks, keeping everything else there. This would be simple, but property-style methods look slightly uglier inside IDEs.
  2. Move the declarations directly inside Phaser.Scene itself and edit the callback types to reference that instead.
// opt 1
declare module Phaser {
  class Scene {
    init?: SceneInitCallback;
    preload?: ScenePreloadCallback;
    create?: SceneCreateCallback;
  }
  namespace Types {
    namespace Scenes {
      type SceneInitCallback = (this: Scene, data: object) => void;
      type ScenePreloadCallback = (this: Scene, data: object) => void;
      type SceneInitCallback = (this: Scene) => void;
    }
  }
}
// opt 2
declare module Phaser {
  class Scene {
    init(this: Phaser.Scene, data: object): void;
    preload(this: Phaser.Scene, data: object): void;
    create(this: Phaser.Scene): void;
  }
  namespace Types {
    namespace Scenes {
      type SceneInitCallback = Phaser.Scene['init'];
      type ScenePreloadCallback = Phaser.Scene['preload'];
      type SceneCreateCallback = Phaser.Scene['create'];
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions