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

feat(docgen): new generator for TypeScript declaration #8

Closed
wants to merge 13 commits into from

Conversation

drauggres
Copy link
Contributor

@drauggres drauggres commented Nov 19, 2019

Output for current master (9.0.0) branch: titanium.d.ts

Pros:

  1. Dictionary (typed factory methods):
const button = Ti.UI.createButton({
 width: 100,
 backgroundColor: '#ddd',
 text: '+',   // TS2345: [..] 'text' does not exist in type [..]
 left: 10
});
  1. Types for "addEventListner", "removeEventListener":
const button = Ti.UI.createButton();
button.addEventListener('click', function(e) {
 this.backgroundColor = 'red';
 this.text = 123;  // TS2339: Property 'text' does not exist on type 'Button'.
});
  1. Types for Events:
const button = Ti.UI.createButton();

// know event type
button.addEventListener('click', function(e) {
 e.source.backgroundColor = 'red';
 console.log(`x: ${e.x}`);
 console.log(`x: ${e.z}`);  // TS2339: Property 'z' does not exist on type 'Button_click_Event'.
});

// unknown event type
button.addEventListener('myEvent', function(e) {
 console.log(e.lol);  // ok, because 'myEvent' is unknown event type -> e: any
});
  1. Modules declared as both namespace and class (with static methods). This allows to define types for specific events for Modules like Ti.Geolocation, Ti.Media, Ti.Network etc.:
Ti.Network.addEventListener('change', e => {
  console.log(e.reason);
  console.log(e.status);  // TS2339: Property 'status' does not exist on type 'Network_change_Event'
});

Cons:

  1. Any custom properties are not allowed (even id Ti.UI.View now have id property):
Ti.UI.createView({
  id: 'myViewId',
  myProp: 'content'  // TS2345: [..] 'myProp' does not exist in type [..]
});

@drauggres
Copy link
Contributor Author

I know, that you already have .d.ts generator, so it's ok if this PR is not going to be merged.
But I would really like to see these features in @types/titanium

Also generated file revealed some mistakes in the documentation (you can find them by #Error comment in it) , so I will refer to this PR in the upcoming TIDOC tickets.

- Dictionary<T> (typed factory methods)
- Types for "addEventListner" and "removeEventListener" methods
- Types for Events
@drauggres drauggres changed the title feat(docgen): new generator for TypeScript declaration WIP. feat(docgen): new generator for TypeScript declaration Nov 20, 2019
@janvennemann
Copy link
Contributor

So from a first quick look I really like these changes and would love to get them in for our existing TypeScript generator. The cons are negligible i guess. If you absolutely need custom properties you can still cast the created proxy to any and assign whatever you want.

@drauggres do you think you can easily adopt these new features to our existing TypeScript generator?

@drauggres
Copy link
Contributor Author

drauggres commented Nov 20, 2019

The cons are negligible i guess.

I have an idea of changing Alloy in a way that it will generate TS classes, and everything (including code generated from xml-views, tss-styles, models and collections) could be checked for types.

Problem is that id is really fundamental property (and somehow it is not in the API). Strict types checking will not allow it and casting to any nullifies the point of typing.
Nevermind: tidev/titanium-sdk#11353

@drauggres do you think you can easily adopt these new features to our existing TypeScript generator?

I just found some flaws in my generator (that's why I changed title to WIP), so I will need more time to process the SDK API structure before even thinking about it (don't think it will be easy).

This will allow to describe event for some special namespace-classes like
Ti.Geolocation, Ti.Media, Ti.Network etc.

https://www.typescriptlang.org/docs/handbook/declaration-merging.html
@drauggres drauggres changed the title WIP. feat(docgen): new generator for TypeScript declaration feat(docgen): new generator for TypeScript declaration Nov 22, 2019
@drauggres
Copy link
Contributor Author

drauggres commented Nov 22, 2019

OK. Now I'm satisfied with the generated result. This is how I would like to see @types/titanium.

do you think you can easily adopt these new features to our existing TypeScript generator?

I will look into it and if I succeed then I'll open a new PR.

@drauggres
Copy link
Contributor Author

drauggres commented Dec 5, 2019

@janvennemann Sorry for disturbing you, but do you know where documentation for Windows platform is coming from?
I don't see it in the SDK and therefore in the generated d.ts.

UPD: nevermind, found it here. BTW current version in DefinitelyTyped is missing "Windows" too.

drauggres and others added 7 commits December 13, 2019 12:41
in case a "namespace" is empty we can skip its generation, but
then we must define constants from it as static properties on
the related class
previously events without additional properties were mapped to "baseEvent"
skip duplicates, mark them as error
add missing "static" for removed static methods
@drauggres
Copy link
Contributor Author

closed in favor of #25

@drauggres drauggres closed this Dec 18, 2019
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.

None yet

2 participants