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

Improving TypeScript Support #151

Open
deevus opened this issue Mar 11, 2021 · 3 comments
Open

Improving TypeScript Support #151

deevus opened this issue Mar 11, 2021 · 3 comments

Comments

@deevus
Copy link
Contributor

deevus commented Mar 11, 2021

I'm loving this package, but the TypeScript types generation is very complicated. Is there a guide on how to contribute types? I understand that the core idea is that the types are generated from JSDoc.

I was hoping to add types for analytics-utils as I am developing a new plugin in TypeScript, however I'm a bit lost as to how that should be done.

@deevus
Copy link
Contributor Author

deevus commented Mar 11, 2021

For reference here is the plugin I have been developing: https://github.com/deevus/analytics-plugin-tapfiliate

I will made a PR to include it in the README once I am happy with it.

@DavidWells
Copy link
Owner

Nice plugin!


Yeah, the typescript setup is odd. The types are generated from JSdoc with a custom template here then post-processed here because the jsdoc template isn't quite right (or I'm holding the phone wrong).

I prefer to keep JSdoc as the main typing mechanism for the time being. If there is a way that typescript tsd will do this for us that would be great. Last I looked (over a year ago) it didn't support generating type definitions from JSdoc.

There is some active discussion over on JSDOC types at https://github.com/voxpelli/types-in-js/discussions


Another typescript issue this project is facing is types for browser implementations vs serverside implementations.

It's possible that browser & server have slightly different settings and thus different types. From what I've seen typescript doesn't support this type of "universal" module setup. More context here & here

If you can help solve this problem, you will be my hero forever.

@deevus
Copy link
Contributor Author

deevus commented Mar 13, 2021

I prefer to keep JSdoc as the main typing mechanism for the time being. If there is a way that typescript tsd will do this for us that would be great. Last I looked (over a year ago) it didn't support generating type definitions from JSdoc.

tsd does support parsing JsDoc since TypeScript 3.7. From my testing it seems very doable. I will look at creating a PR for it.

Another typescript issue this project is facing is types for browser implementations vs serverside implementations.

On some level I think it is possible, but how do you imagine it working? How does the programmer's editor know that they are writing node or browser code? The easiest way would be to have the user import the specific browser/node analytics plugin.

You could have the root type be something like AnalyticsBrowser | AnalyticsNode. If you want it to be isomorphic the programmer might have to cast it like:

const analytics = new Analytics(...) as AnalyticsBrowser;

How different are the interfaces?

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

No branches or pull requests

2 participants