phaser icon indicating copy to clipboard operation
phaser copied to clipboard

SceneManager.getScene function to generic

Open rollinsafary-inomma opened this issue 3 years ago • 3 comments

Version

  • Phaser Version: 3.54.0
  • Operating system: Windows 10
  • Browser: Google Chrome

Description

Hi there, have suggestion to change SceneManager.getScene() type def to generic

Example Test Code

I mean change from

getScene(key: string | Phaser.Scene): Phaser.Scene{
...
}

to

 getScene<T extends Phaser.Scene>(key: string | T): T {
...
}

Additional Information

This will help us in coding in TypeScript now

const scene: MyScene = sceneManager.getScene(MyScene.NAME) as MyScene

then

const scene = sceneManager.getScene(MyScene.NAME);

rollinsafary-inomma avatar Apr 19 '21 08:04 rollinsafary-inomma

As far as I know, Phaser typescript definitions are auto-generated from the jsdoc and it doesn't support generics

vforsh avatar Apr 19 '21 12:04 vforsh

If you try this:

    /**
     * Retrieves a Scene.
     *
     * @method Phaser.Scenes.SceneManager#getScene
     * @since 3.0.0
     *
     * @generic T extends Phaser.Scene
     *
     * @genericUse {T} - [key,$return]
     *
     * @param {(string|*)} key - The Scene to retrieve.
     *
     * @return {*} The Scene.
     */

We get getScene<T>(key: T): T; so only thing that is missing is the extends ... and that's because the parser doesn't currently support setting baseType prop. Which I guess, should be pretty trivial to add here: https://github.com/photonstorm/phaser/blob/master/scripts/tsgen/src/Parser.ts#L607

xmahle avatar Feb 27 '22 19:02 xmahle

Hah, this works:

/**
 * Retrieves a Scene.
 *
 * @method Phaser.Scenes.SceneManager#getScene
 * @since 3.0.0
 *
 * @generic {Phaser.Scene} T [key,$return]
 *
 * @param {(string|T)} key - The Scene to retrieve.
 *
 * @return {?T} The Scene.
 */

result:

/**
* Retrieves a Scene.
* @param key The Scene to retrieve.
*/
getScene<T extends Phaser.Scene>(key: string | T): T;

xmahle avatar Mar 01 '22 17:03 xmahle

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

photonstorm avatar Nov 17 '22 22:11 photonstorm