quinton-ashley / p5play

JavaScript game engine that uses q5.js/p5.js for graphics and Box2D for physics.
https://p5play.org
GNU Affero General Public License v3.0
667 stars 184 forks source link

p5play.d.ts has 2 conflicts with @types/p5 definitions. #276

Closed christopherball closed 1 year ago

christopherball commented 1 year ago

I've attempted to incorporate p5play v3.16.0 into my minimal starter template that already enables full autocomplete for p5.js and typescript in VSCode. I am unable to get the p5play.d.ts to compile cleanly in my project as it has two conflicts (log and camera) with the core p5.js d.ts.

image

I tried getting this to work by converting my code to global mode (since I didn't see instance support for p5play) to no avail. Beyond the compilation conflicts which looks like they need to be fixed in the p5play.d.ts (?), is there a reason it isn't rolled into @types/p5 or at least @types/p5play?

quinton-ashley commented 1 year ago

I want to help but I'm not that familiar with type definitions.

There's no @types/p5play the type defs for p5play are just in the p5play.d.ts file inside the p5play npm package.

What are you trying to do? Where are you getting the error log from? I haven't seen those messages before.

The conflicts are because in p5play I made log a shortcut for console.log. I bet p5.js has log as a shortcut for Math.log.

Also p5play needs to override the p5.js camera, which I think is only for webgl.

christopherball commented 1 year ago

There's no @types/p5play the type defs for p5play are just in the p5play.d.ts file inside the p5play npm package.

Yeah I know - I was proposing you migrate your p5play.d.ts into the @types (definitelytyped) project as most developers are used to pulling typescript declaration files for various projects from that single source (just as p5js's are also stored there).

What are you trying to do? Where are you getting the error log from? I haven't seen those messages before.

The easiest way to replicate this is If you git clone ... npm install my boilerplate project I linked above that offers full autocomplete with p5.js and typescript, pop open VSCode, now simply npm install p5play on top of that and try to see if you can get autocomplete to work for p5play (which VSCode will try to reference your p5play.d.ts file) whilst not breaking autocomplete for standard p5.js. You'll see these typescript compilation errors when you trigger a tsc via npm start in my boilerplate project's case.

With respect to conflicts (log and camera), this currently means nobody can tsc their typescript if their project uses both p5play and p5 in typescript. Ideally p5play should offer an "instance mode" flavor like p5js does so that we don't run into conflicts.

quinton-ashley commented 1 year ago

I'm not going to make a separate @types repo for p5play because I like having the type defs in the same package. Also, my perception was that doing it the other way is outdated since vscode and npm can check for types within packages.

The p5play.d.ts file only exists to provide auto-complete in Visual Studio Code. Check out the p5play-template project to see how it can be done: https://github.com/quinton-ashley/p5play-template/blob/main/jsconfig.json

Are you trying to use p5play with TypeScript?

I gotta be honest, I'm not a big fan of TypeScript and I don't know much about it. Personally, I have no plans to make p5play compatible with TypeScript or provide any example documentation in TypeScript.

p5play is fully compatible with p5.js instance mode. That's how all the examples on p5play.org are run.

I don't see what instance mode has to do with the conflicts but if you want to get the TypeScript defs to be applied to p5.js instances you can try it. p5play is written using many modern JavaScript features and the type files are generated via JSDocs. Unfortunately it seems tsc doesn't support parsing modern JavaScript though, which is why, for generating type defs, I edit the p5play.js file before having tsc process it. I cut out the p5.js instance mode stuff and made all p5play's classes file level. It's in the build.js file in this repo: https://github.com/quinton-ashley/p5play-types

I'm going to close this issue as "not planned" but you can still respond in this thread and I can try to help.