-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
Contribute TypeScript type files for most Three.JS JavaScript files #15597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks @LuWangThreekit! From #11552 (comment), is it possible to automate testing this somehow? Against the source files themselves, or our existing examples and tests files, perhaps? |
Hi @donmccurdy, I have never done automate testing before. I will do some search to see how I can achieve this. Is there any chance that you can give me some directions? |
|
To be honest I don't know for sure whether it's possible, but it would be helpful to find out before we take ownership of the TS typings. Based on this documentation it's possible to compile JS while checking it against typings and JSDoc. The three.js codebase does not use JSDoc, so practically that may or may not work... ideally we'd be able to do something like this... ...and a failed compile would tell us that the typings have gotten out of sync with the core library. |
Thanks! I will check to see if this will work. |
|
BTW to be clear, this is based on the existing typings here: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/three Lu wrote an automated method to split it apart. |
|
If three.js is not using TS or planning to move to TS, and if we have no way to verify that these types remain correct with library changes, I'm not sure we should be taking over their maintenance. The existing contributors to the DefinitelyTyped are presumably using TypeScript themselves and can check the types against their own projects, which I think means they will do a better job than we are likely to do. I worry that by just moving the types here, without an owner or a plan to maintain them, we're essentially saying, "don't worry, we've got this" to the current maintainers without a good chance of success. That said, I appreciate the effort here to improve workflows for TS users, and would welcome ideas on how to make that robust and sustainable. 🙂 |
|
I agree with @donmccurdy. I think the |
|
As a consumer of three.js that uses At least if the type definitions were in the same repo, one could change them side-by-side, and incrementally move towards automated tests and generated method signatures in the documentation, instead of a duplication of effort that doesn't really seem like it should be done by humans. With regard to adding an extra burden to the maintainers of three.js, that cost is already included in the expectation that the documentation will be complete, correct, and up to date. It's just that currently the validation of such is done by humans filing bug reports, rather than by a type checker. In practice, this burden is shifted out to the users, and to TypeScript users (judging from the submissions to DefinitelyTyped there seem to be many) who have to maintain the definitions in a separate repo. These extra costs do not scale very well, as they are reactive rather than part of the CI process, and it's often not clear whether your program is wrong, the typings are wrong, or three.js has a bug. I also feel certain that if three.js moved towards augmenting their API with typings, it would expose trivial bugs in both the implementation and documentation. if ( ! ( geometry && geometry.isGeometry ) ) {
console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
return;
} |
|
@sethk You would still have to That would be great if there could be some sort of validation of the TypeScript definitions or if the TypeScript definitions were used to generate the documentation in order to reduce redundancy. I feel like the TypeScript definitions would be redundant until that tool is created. My main concern is how long it takes for three.js PRs to get merged and published. With DefinitelyTyped, I can make a change, make a PR, and have it approved, merged, and published so that I can use it, usually within 2-3 days. It would be great if the three.js contributors/maintainers are all willing to keep the TypeScript definitions up-to-date as they make changes, but it just adds another level of barrier to have to know how TypeScript works. At least in the earlier days of TypeScript, there weren't enough people who felt confident in their TypeScript knowledge to make this work on some repos. I would be much more excited if we actually started converting some of three.js to TypeScript to reduce redundancy even more. Or if we started by adding JSDoc and then added the TypeScript definition files then we could avoid redundancy there. |
My main issue is the denormalization of the Three.JS API contract into three separate places. All the yak shaving I had to go through in order to get my project to compile is just a symptom of that. Using Anyway, all of that was a pretty high bar for just using Three.JS from a TypeScript app, and if I hadn't already felt very committed to using TypeScript for the rest of the app, I probably would have abandoned it.
They are definitely redundant, but at this point they're redundant and also not colocated.
Yeah, that is a valid consideration. However, it seems like PRs to Three.JS that introduce new APIs or change old ones must also include a step to update the documentation, which would be a natural place to update the TypeScript declarations as well. Separate PRs that update only the typings should be pushed through fairly quickly, along the lines of a low-risk bugfix.
For the less polymorphic APIs, this doesn't seem like it should be that big of a hurdle. If a person making an API-level change understands the full implications of the change they are making, they should be able express this using normative type declarations, rather than the informal type signatures used in the documentation.
I agree, but a conversion seems like it would require a lot of buy in. A combination of JSDoc and TypeScript definition files to replace the existing documentation seems like a good stepping stone. What if the documentation could be generated from JSDoc comments in the definition files? My whole point is: please don't abandon this unification effort just because the intermediary steps seem redundant! |
|
@sethk You have persuaded me that this is probably worth it. I played with TypeDoc a little bit to see how hard it is to generate documentation from TypeScript definition files. From what I can tell, it would take quite a bit of work (custom tooling) to generate documentation from the TypeScript definitions that is the same as the current documentation. One thing that comes to mind that might be a problem are the Chinese translations of the documentation. I'm not sure if there's any way to provide both English and Chinese translations of the documentation and it doesn't seem like both of those should go in either JSDoc or TSDoc comments. |
|
@donmccurdy Sorry for the delay. Can I assume that all types are correct if all the existing examples can run without raising any error in the console? If so, I have written a script to test all the examples. 98% of the examples passed. Those which fail the test are because of @types/three also does not have the declaration for them. |
|
@donmccurdy Thanks for the idea of checking Three.Js against its TypeScript definitions. It actually works really well. Here is the results I got: https://gist.github.com/LuWangThreekit/962d60e5b2d44aa898e4174c77bdbdb4 I used this command line: Thus there was actually a bunch of errors in the DefinitelyTyped Three.js project. But it is trivial now to identify them and fix them. I do not think any of those are in Three.Js proper though, but maybe if I can fix these it will reveal more. The issue with running tsc against the examples files is that they do not include their dependences as they use very old style JavaScript. If we could modernize their code, then we could do the same approach. I think that once I get these errors fixed, we could just add a simple PR checker that continues to keep these updated for the main part of Three.JS. I wouldn't be surprised if it catches obvious bugs in Three.JS going forward. |
|
That's great to hear, thanks @LuWangThreekit! If the type declaration tests are running against our source but not the examples folder, that's totally fine with me. In the future it would be great to (1) clean up existing issues in the typings, and (2) get the test running against example code as well, but I don't think either is necessary in this PR. With that test functional, I have no objection to bringing the typings into this repository. 👍 |
|
@donmccurdy I just fixed all the errors which reported by |
|
@LuWangThreekit Good work! Can we add that command to the CI build so that it will test it whenever a PR is made? |
For my $0.02 we can leave that for a future PR; for a decision about whether to merge this PR I think it's enough to know that it can work in CI. :) |
|
@Methuselah96 I think we can use "tsc" build the typed threeJS to regular Js style then use CI to build the Js one. |
|
I agree with @donmccurdy. We should just try to get this PR merged as soon as possible and then we can add the build step in a follow-up PR. |
|
@mrdoob, @WestLangley What are your thoughts on this PR? |
|
@bhouston I don't have an opinion on this one. |
|
Lets give it a go! 👍 |
|
Thanks! |
|
I think we should move @LuWangThreekit would you like to do a PR with these changes? |
|
@mrdoob I will do it now and make new PR. Thank you for marge my PR. |
|
@mrdoob @donmccurdy @Methuselah96 we've researched having the types in a separate directory, but it appears to be less than optimal. A separate directory for types does not support a folder structure from our research, thus it is 100+ files in a flat directory and the would have a different include structure as well (as it would have to be mapped to the same directory.) Personally, I think that it is best to leave them side-by-side for manageability because then it fully mirrors the main Three.js files. It would also serve as a reminder to update them. Although I know this isn't optimal if you want to hide the *.d.ts files in their own directory. But maybe to make TypeScript easier to maintain @LuWangThreekit could contribute a tsconfig.json file (so one just needs to run |
|
@bhouston I see... |
That sounds good. |
|
@LuWangThreekit @bhouston Is there a reason there is both a |
|
@mrdoob @Methuselah96 |
In reference to the previous conversation involving @bhouston and @mrdoob, I've created Typescript type files (*.d.ts) for most JavaScript files in the Three.JS project.
See here for the motivator: #11552 (comment)