Fix TypeScript typing for filterColor shader hook#8644
Fix TypeScript typing for filterColor shader hook#8644Kathrina-dev wants to merge 13 commits intoprocessing:dev-2.0from
Conversation
35fba48 to
bd57864
Compare
bd57864 to
a5f85a7
Compare
davepagurek
left a comment
There was a problem hiding this comment.
Thanks for taking this on @Kathrina-dev! While the original issue was for filterColor, I think this actually applies to all the hook objects documented around filterColor in the code. While we could add a patch method for each one, this seems likely to drift from the documented properties, especially as we add new hooks. It might be worth instead taking the approach of updating the typescript generation script to work with JSDoc typedefs (or something else, like documenting a pretend class for each hook object or something?) so that we can have the jsdoc be the sole source of truth for both documentation and types.
|
Thanks for the suggestion, I've already removed the formatted part of the code to make it easier to identify the diff. My initial approach was to use Typescript generation script (@Property, @typedef) but it kept collapsing the JSDoc typedef into an object. I agree that adding a patch would cause a drift from the documented properties, so I'll try the alternative way you've suggested and let you know the results. |
|
Hi! If you run Looks like the way the existing typedefs are being exported has changed -- the typedef itself should be a type, but also there should be a const of the same name from the rest of the jsdoc. |
|
I think the issue was that some constants (like BLEND, ADD, etc.) weren’t being picked up properly. I updated the lookup to include them and adjusted how they’re handled in the type conversion depending on where they’re used. That seems to fix the errors. Thanks for being patient with me, I'm new to contributing to Open Source and forgot to run the test cases before committing. I'll keep it in mind next time. |
davepagurek
left a comment
There was a problem hiding this comment.
Thanks for making some updates, good to see the tests passing now! It looks like although they past, the generated type files still aren't 100% correct. I left some comments, one of which includes some steps I'd follow when working on this to help verify the change.
| * @property {any} canvasSize | ||
| * @property {any} texelSize | ||
| * @property {any} canvasContent | ||
| * @property {function(): undefined} begin |
There was a problem hiding this comment.
Should these be : void too or is there a reason for them to be : undefined?
There was a problem hiding this comment.
When I tried using void, the generated p5.d.ts ended up showing any instead. I tried using different ways to define a function but it still kept showing any. So I've defined it as undefined which showed up correctly while generating p5.js.d.ts
|
Thanks for taking out the time to leave those reviews, I'll look further into everything you've mentioned. This issue is a lot bigger than I expected but I'm very thankful for your help in this matter. |
|
Considering that this issue requires alot more modifications to the I'm considering to revert the changes back to how it was before and then creating a seperate function to handle Object types parsing. Since we already have separate functions for classes and methods, I think doing the same for typedefs will make it easier to handle things like function properties and avoid affecting other type conversions. This will make the What are your thoughts on this approach @davepagurek ? |
|
That sounds ok to me @Kathrina-dev! Agreed that this gets a little hairy, thanks for your patience working on it! |
|
@davepagurek I've added the new function to handle typedef properties. It passes the The only concern I have is about leaving the |
Resolves #8574
Changes
Fixes incorrect TypeScript typing for the
filterColorshader hook.Previously, the generated TypeScript definitions declared:
declare const filterColor: object;
Because of this, accessing documented properties such as:
filterColor.texCoordfilterColor.canvasSizefilterColor.begin()filterColor.set(...)resulted in TypeScript errors like:
Property 'texCoord' does not exist on type 'object'.
Other Methods attempted at resolving the error
Initially I attempted to resolve this through JSDoc annotations in the source by using:
@property@typedefto explicitly define the structure of
filterColor.However, the documentation generator used in the build pipeline still produced the type as
objectin the generated TypeScript definitions. Because of this limitation in the JSDoc → TypeScript generation step, the correct structure was not preserved.Solution
To ensure the generated typings match the documented API, I added a patch in:
utils/patch.mjs
This patch updates the generated declaration to:
This aligns the TypeScript definitions with the documented properties of the
filterColorshader hook and prevents TypeScript errors when accessing them.Screenshots of the change
N/A (TypeScript typing fix)
Suggested Reviewers
PR Checklist
npm run lintpasses