Babylon.JS & TypeScript - Inherited members not visible on derived classes?

Hello, thanks for popping in to help.

Context:

I’ve just started using Babylon.JS with TypeScript. I’m reasonably comfortable with TypeScript syntax and expectations, but there’s a solid chance I’m missing something simple or obvious; I don’t have native common sense yet!

I have plenty of experience with type-safe languages however.

Problem:

I’ve been having trouble accessing inherited members, with TypeScript spitting out errors like:

(thing as Texture).readPixels();
    // Property 'readPixels' does not exist on type 'Texture'. ts(2339)

(thing as BaseTexture).getSize();
    // Property 'getSize' does not exist on type 'BaseTexture'. ts(2339)

Edit: This is Visual Studio Code’s TypeScript language support, my project does build and run as expected.

I’ve been researching other threads and found cases where (for example) a user is attempting to access material.albedoTexture, where material is Material and so the albedoTexture property is not visible without a cast.
The type is boxed; this behavior is inline with my expectations of type-safe languages.

However in my case, these inherited members should be known to exist statically, and are “safe”.
Is it really expected that I perform casts to base classes when accessing inherited members?

Could I be missing something here?

Hey,
This is quite interesting. Casting to Texture should get you the function you are looking for, especially since readPixels is available in the BaseTexture class, which is a parent of the Texture class.

Would you be able to somehow reproduce this in a shared project? Something I can load locally and check myself?

Ah, my apologies:
Upon further investigation, it seems that the error I’m seeing is coming from Visual Studio Code’s TypeScript language service.

I’m not actually seeing any errors when the project is built, either for development or production, so that’s good at least.

I will prepare a reproduction repo all the same!

Reproduction repository here: GitHub - SteffanDonal/typescript-inherited-members-fun

1 Like

i’ll check it later today

1 Like

Oh, just one thing - I noticed you are using an older version of the framework (7.49) with nodenext as module resolution in your tsconfig.
We added support for nodenext, but only after 8 was released. Can you try with a later version of the framework? or move to node module resolution in your tsconfig?

1 Like

I can confirm, updating to version 8.5 fixed the issue - thank you!

2 Likes

great!! love to hear that.

3 Likes