Skip to content

Conversation

davepagurek
Copy link
Contributor

@davepagurek davepagurek commented Sep 26, 2025

Resolves #7862
Resolves #7938

This builds off of #7863 and refactors how typescript type generation is done, and adds integration tests to make sure sketches that use the types all work correctly. We can add new .ts files into the test/types folder to verify that they work correctly.

  • Rather than a duplicate implementation, I merged a lot of code with the reference generation. Both use the same data processor to read the Documentation.js output now, and each passes in a separate strategy for walking type info, as typescript types look different than the syntax we use in the reference.
  • Rather than trying to preserve 1:1 relationship between source files and .d.ts files, this generates a single p5.d.ts (and a global.d.ts.) We can split this up further in the future if we really need, but this makes class generation a lot less brittle.
  • I caught and fixed a number of small bugs in Documentation.js resulting from the placement of our block comments or copy and paste errors.

TODO:

  • Add back in patches for generic types
  • Better types for callback function parameters Update: probably going to leave this for now since it'll affect the reference generation too. Future PR maybe!

PR Checklist

@davepagurek
Copy link
Contributor Author

I think feature-wise, this is good to go now. @limzykenneth @ksen0 let me know if the updates to type exporting, to the test CI action, and to the dependencies (adding typescript as a dev dependency for testing) make sense!

@davepagurek
Copy link
Contributor Author

ok at least for my company's usage, this gets me to 0 type errors compared to before. there may still be room for improvement because the 1.x types were lacking sometimes too, but I feel like this is at least enough for us!

@davepagurek
Copy link
Contributor Author

davepagurek commented Oct 2, 2025

Noticed one more thing I was missing: types for p5.strands GLSL methods. So far the types for those are pretty unspecific, e.g. getTexture(param0: any, param1: any): any, because it'll take a bit more thought to figure out the best way to type p5.strands objects. They act in many ways like regular numbers sometimes, but also have extra methods on them. You can create vectors like arrays, but then those get extra properties like .xy. We also support + between those without them turning into a string. (Maybe the best way forward is to patch methods onto number?) There are also some things like Sampler2D which need a new type. So for now they're all just any and I can make a new issue for someone to improve this further in the future.

One other typing speedbump: p5.strands/GLSL have a length(vec) method. Adding a global type for this is tough for TypeScript because length already exists on window. For now I'm just ignoring outputting that method. We might want to make an alias for length for TS users since we can't easily "undo" the existing length property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants