Skip to content
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

Generate TypeScript definitions #2279

Merged
merged 9 commits into from
Jan 4, 2018
Merged

Generate TypeScript definitions #2279

merged 9 commits into from
Jan 4, 2018

Conversation

Zalastax
Copy link
Member

@Zalastax Zalastax commented Oct 21, 2017

I've added a grunt-task that generates TypeScript definitions from the YUIDoc. It's probably not perfect, but on a glance it looks fine.
It uses the code by @toolness from http://processing.toolness.org/general/2016/03/16/typescript-to-the-rescue.html and depends on and includes the fixes in processing/p5.js-sound#222.
Types that can't be modelled using YUIDoc could be manually modified using declaration merging.
I can maintain the type-generation in the future if there are missing features / enhancements needed.
Closes #1392

@Spongman
Copy link
Contributor

I have a heavily modified version of that script that handles some of the typescript overloads and missing types better...

@Zalastax
Copy link
Member Author

@Spongman it would be awesome if you could share the code here so I can incorporate it 😊

@Spongman
Copy link
Contributor

Spongman commented Oct 24, 2017

yeah, i need to clean it up first so it fits better with what you have.

btw: here's what it generates: https://p5ide.azurewebsites.net/assets/p5.d.ts

@Spongman
Copy link
Contributor

ok, i'm sorry, i have no idea how to get github to send a PR from my fork to yours, but here's my branch:

https://github.com/Spongman/p5.js/tree/typescript

@Zalastax
Copy link
Member Author

Awesome @Spongman! I cleaned up your commits, which I hope is OK with you. I think this is ready to merge now, anyone I need to ping?

@Spongman
Copy link
Contributor

i would exclude the p5.sound stuff from this PR as that's already been merged in another PR.

@Zalastax
Copy link
Member Author

Zalastax commented Nov 6, 2017

@Spongman anything else we need?

@Spongman
Copy link
Contributor

Spongman commented Nov 6, 2017

yeah, you might want to merge with my fork again: https://github.com/Spongman/p5.js/tree/typescript. i added the param & return descriptions to the .d.ts doc comments.

i have no idea how to send a PR directly between different forks.

@Zalastax
Copy link
Member Author

Zalastax commented Nov 9, 2017

@Spongman time to bother you again 😁
I'm fairly happy, but there are some errors left to solve:

  • in global-mode.d.ts, we have duplicate identifier log
  • in d.ts, we have Class 'Noise' incorrectly extends base class 'Oscillator'
  • MISSING: SoundObject
  • MISSING: Tone.Signal

@Zalastax
Copy link
Member Author

@Spongman fixing the MISSING definitions solved the rest of the problems. I don't think there is a pretty solution to those definitions so I'm OK with hard-coding them. Merge ready?

@Spongman
Copy link
Contributor

it might be worth looking into how these would be deployed. there's some grunt scripts to do the deployment - see if you can work out how to place these alongside the .js files...

@emo-eth
Copy link

emo-eth commented Nov 29, 2017

One thing I have noticed playing around with this script & others (at least last I checked, so forgive me if this has been resolved) is that it doesn't seem to include type definitions for the p5 constructor in instance mode, even though the constructor specs do seem to be in the json YUIDoc spits out.

@Spongman
Copy link
Contributor

i fixed the missing p5 constructor in two commits:

@Zalastax
Copy link
Member Author

Zalastax commented Nov 30, 2017

I had a look at the grunt tasks.
release-bower.js: doesn't make much sense to publish TS on bower.
release-docs.js: I don't think we need to change anything here. If we want to reference the types from the docs we should upload them as an asset on github.
release-github.js: This looks like the only change we might want to make. Does it make sense to add the typescript definitions as release assets? I've never seen that before. IF we want definitions as a release asset, what should its mime type be?
release-p5.js: already works and includes both .d.ts, both on npm and in the zip.

What do you think?

@Spongman
Copy link
Contributor

Spongman commented Dec 6, 2017

yeah, i think adding them to the github release makes sense. (ec7d563)

also, automatically submitting a PR to DefinitelyTyped might be nice.

@Spongman
Copy link
Contributor

Spongman commented Dec 7, 2017

ok, so it's not just my PRs that are failing eslint:test on travis. do the tests pass on your local machine?

@Zalastax
Copy link
Member Author

Zalastax commented Dec 7, 2017

They did not pass on my local machine, but the problem was still introduced by changes on master. Master has fixed that now though, so tests pass on my machine again.
It'd be nice to have Travis re-run the tests before accepting the actual merge, but status quo seems to work well in practice so I won't bother looking into that.

@jeffreykaufman
Copy link

Hey there, just wanted to pop in and ask how this project was going. I've been using p5 in JavaScript for awhile now, and would love to see a definition file created for it.

@Spongman
Copy link
Contributor

Check my comment here for a recent .d.ts file: #1339 (comment)

Zalastax and others added 9 commits January 1, 2018 22:26
Improves error reporting.
Correctly emits readonly properties.
Tone.Signal is a type from another library so we can't "see" its type. It's only used as a return from a constructor, but constructors can't have return types in TS so we don't even emit a definition for it.
SoundObject is either p5.Sound or an object that has .connect(unit) (e.g. Amplitude, AudioNode). YUIDoc doesn't seem to have free-standing interfaces so expressing "has .connect" seems hard.
@lmccart
Copy link
Member

lmccart commented Jan 4, 2018

this is fantastic. sorry it took so long to review.

@lmccart lmccart merged commit 1acf1e3 into processing:master Jan 4, 2018
@Zalastax Zalastax deleted the typescript branch June 15, 2018 09:37
@dgennetten
Copy link

Is there any guidance documentation for getting started with TypeScript on p5?

@Zalastax
Copy link
Member Author

Zalastax commented Nov 2, 2018

@dgennetten we have gathered some example projects in ps.ts. I hope they can get you started! Otherwise, open an issue over there.
Typings are available via the @types/p5 package but there needs to be documentation in the main repo/website in place as well (if they are not already).

@cupcakearmy cupcakearmy mentioned this pull request May 14, 2021
17 tasks
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.

Hope there will be a place for typescript definition file of p5js
7 participants