-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
First-party TypeScript definitions #3166
Comments
@maxkfranz We'd love to help out with this one. We're keen TypeScript users (and big fans of JSDoc as an incremental step there). I'd imagine it's going to be trickier than estimated in nrnb/GoogleSummerOfCode#94 though, because of the style of JavaScript that the codebase is written in. The heavy use of constructor functions (as opposed to actual JS classes) and dynamically building prototypes out of mixins represents a bit of a challenge for TypeScript's JSDoc support. The usual escape hatch with this kind of dynamic JavaScript is to drop back to Either way, I can bring a good amount of hands on experience with TypeScript + JSDoc if that's useful here. Just let me know how you'd like to approach it! |
It was, but I think that's been mostly sorted.
Yes. Some of the annotations in the
Great. A good first step would be to explore and experiment with the |
Although it looks like the doc comment coverage of the I guess the annotations were written with producing the documentation in mind, without much focus on whether the resulting TypeScript was correct. There doesn't seem to be a Here's an example of the type of semantic problem that is very common here. These are the annotations for /**
* @typedef {object} cy_autolock
* @property {object} NULL
* @property {object} bool - A truthy value enables autolocking; a falsey value disables it.
*/
/**
* Get or set whether nodes are automatically locked (i.e. if `true`, nodes are locked despite their individual state).
* @memberof cy
* @path Core/Viewport manipulation
* @param {...cy_autolock} bool - Get whether autolocking is enabled. | Set whether autolocking is enabled.
* @methodName cy.autolock
*/
autolock: function( bool ){
if( bool !== undefined ){
this._private.autolock = bool ? true : false;
} else {
return this._private.autolock;
}
return this; // chaining
}, And here's the TypeScript it generates: declare class cy {
/**
* Get or set whether nodes are automatically locked (i.e. if `true`, nodes are locked despite their individual state).
* @param bool - Get whether autolocking is enabled. | Set whether autolocking is enabled.
*/
static autolock(...bool: cy_autolock[]): void;
}
/**
* @property bool - A truthy value enables autolocking; a falsey value disables it.
*/
declare type cy_autolock = {
NULL: any;
bool: any;
}; There are quite a few individual problems here.
Compare to the handwritten types at /**
* Get whether nodes are automatically locked
* (i.e. if true, nodes are locked despite their individual state).
* http://js.cytoscape.org/#cy.autolock
*/
autolock(): boolean;
/**
* Set whether nodes are automatically locked
* (i.e. if true, nodes are locked despite their individual state).
* http://js.cytoscape.org/#cy.autolock
*
* @param bool A truthy value enables autolocking; a falsey value disables it.
*/
autolock(bool?: boolean): this;
Ideally people would transition from Here's how the comments would need to look to generate the correct types. /**
* Set whether autolocking is enabled.
* @overload
* @param {boolean} bool A truthy value enables autolocking; a falsey value disables it.
* @return {Core}
*
* Get whether autolocking is enabled.
* @overload
* @return {boolean}
*
* Get whether nodes are automatically locked (i.e. if `true` nodes are
* locked despite their individual state).
* @this {Core}
* @param {boolean | void}
* @return {Core | boolean}
*/
autolock(bool){ // <-- I had to change from `autolock: function` so the @overload tag would work I'm not just picking on the The generated TypeScript declarations also contain a bunch of strange syntactic anomalies. /**
* Get a new collection containing clones (i.e. copies) of the elements in the calling collection.
*/
static elesfn.clone(): void;
/**
* Get whether the element has been removed from the graph.
*/
static elesfn.removed(): void;
/**
* Get whether the element has been removed from the graph.
*/
static elesfn.inside(): void; The There are other strange artifacts that seem to have confused the code generator, producing unhelpful types and comments. /**
* @property selector - The selector the elements should match.
* @property selector - The selector the elements should match.
* @property selector - The selector the nodes should match.
* @property selector - The selector the edges should match.
* @property selector - The selector the elements should match.
* @property filter_callback - The filter function that returns true for elements that should be returned.
*/
declare type cy_$ = {
selector: any;
selector: any;
selector: any;
selector: any;
selector: any;
filter_callback: (...params: any[]) => any;
}; My gut feeling reading through this is that the comments were steered towards working with I think if the primary goal here is to generate first party types, then it's going to be more work to fix these than it is to go from scratch using Happy to kick off that process, but would probably suggest breaking it down into some smaller chunks for PRs, rather than trying to land one huge one for everything. Might as well float this question too, is there any interest in moving towards more modern JavaScript for this project generally? |
Sounds good.
Yes. |
This issue has been automatically marked as stale, because it has not had activity within the past 14 days. It will be closed if no further activity occurs within the next 7 days. If a feature request is important to you, please consider making a pull request. Thank you for your contributions. |
Ref: First-party TypeScript definitions #3166
Ref: First-party TypeScript definitions #3166
Followed up in #3306 |
Description of new feature
What should the new feature do? For visual features, include an image/mockup of the expected output.
Add first-party TS support in the library.
There was a GSOC project that started this out, in the typescript branch. The approach in that project was:
The following tasks are required:
N.b. this would not involve porting Cytoscape to TS. The library would remain JS but would have native TS support for consumers.
Motivation for new feature
Describe your use case for this new feature.
For reviewers
Reviewers should ensure that the following tasks are carried out for incorporated issues:
unstable
branch via pull request. The corresponding pull request is cross-referenced.The text was updated successfully, but these errors were encountered: