melonjs / melonJS

a fresh, modern & lightweight HTML5 game engine
https://melonjs.org
MIT License
5.93k stars 643 forks source link

Type errors and missing properties #1168

Closed russellsteadman closed 7 months ago

russellsteadman commented 1 year ago

Describe the bug

When I type checked my files, I noticed quite a few type errors due to inconsistent typings. For example, Renderable's body references the JavaScript DOM document.body type because the type from physics/body.js is not imported.

To Reproduce (Example)

  1. Create an Entity.
  2. Add the Entity to game.world with the addChild method.
  3. Type check the file, Entity.body does not implement Renderable.body.

Expected behavior No type errors for the action.

Version:

russellsteadman commented 1 year ago

It might be useful to switch the source files to TypeScript, and then the issues would be able to be caught automatically and the import type MyDefaultType from ... and import {type MyType} from ... syntax could be utilized. I can make a pull request with these changes if that is something the maintainers would be interested in.

obiot commented 1 year ago

I really have no idea on how to fix this, as it does sound like a consequence of switching to separate files when generating the TypeScript definitions files (@see #1162)

but definitely there is no intention to switch the whole source code to TypeScript for now. Maybe an "intermediate" solution would be to have an additional TypeScript within the source file that would contains type definition for all major public classes ? and that files would then be added to the batch of automatically generated d.ts files ? can this work ?

@lartkma by any chance, do you have any idea/suggestions ?

russellsteadman commented 1 year ago

The next.js configuration template file has a good example of importing types in JSDoc for pure JS files.

If you don't want to convert the source to TypeScript, it may be good to implement some tests in TypeScript and simply see if the test files pass type checking. This will help with debugging.

obiot commented 1 year ago

that interesting ! Since you were proposing it earlier, would be interested in making a pull request for it ? I'm asking as I'm not personally using TS, this way you can verify directly that this working for you ?

obiot commented 1 year ago

note that we are using webdoc (https://www.webdoclabs.com) as well for the documentation, which is supposed to also support some TypeScript tags or semantic (not sure how to put it). Now I never tried, but if some are missing @ShukantPal can hopefully help :)

this to say as well that if you do make a pull request and do it by modifying the documentation tags, make sure the doc generation is not broken. just use npm run doc-local and make sure it does not throw you any errors.

thanks a ton!

obiot commented 7 months ago

closing this one , see #1199