Question about Typescript definitions

The Phaser.Scene class can optionally be provided create() and preload() methods. But those methods are not explicitly listed in the Typescript definitions.

That creates an issue for me because I have a case where I want to override those on the fly, and Typescript will complain if I say something like:

mySceneExtension.create=()=>{createMethodForMySceneExtension()}

because Typescript won’t realize that create() is one of Phaser.Scene’s methods.

Anyway, I just added create() and preload() methods to the Typescript definitions and everything works the way I want and I’m happy.

I just don’t use JS very often and I want to ask what the rationale would be for not explicitly including those methods in the TS definitions.

:wave:

It’s because Phaser.Scene doesn’t have those methods.

1 Like

Thank you for taking the time.

This is really more of a Javascript question then. Can I bother someone to help me understand how a Phaser.Types.Scenes.SceneCreateCallback like create() gets called by a Phaser.Scenes.SceneManager if create() isn’t a method of the Phaser.Scene? The docs say that create() is an optional method.

So for the purposes of Typescript does it make sense to put preload() and create() in as methods since that gives the option to override, which looks like the original intent. The Typescript definitions are really just a contract about what can be supplied, and preload() and create() can both be supplied, even if just optionally.

From the perspective of people who are good at JS or TS, is it really weird to set the create method this way, as sceneExtensionInstance.create=()=>{createMethod()}?

The Scene Manager calls a scene’s init(), preload(), create(), and update() only if they exist.

You could add empty methods to your scene class, or you could just ignore the TS error.

It’s fine to swap methods if you want to. I think you can just do

 sceneExtensionInstance.create = createMethod;
1 Like