phaser
phaser copied to clipboard
SceneManager.getScene function to generic
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);
As far as I know, Phaser typescript definitions are auto-generated from the jsdoc and it doesn't support generics
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
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;
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.