analytics icon indicating copy to clipboard operation
analytics copied to clipboard

Improving TypeScript Support

Open deevus opened this issue 3 years ago • 3 comments

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 avatar Mar 11 '21 05:03 deevus

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.

deevus avatar Mar 11 '21 05:03 deevus

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.

DavidWells avatar Mar 11 '21 06:03 DavidWells

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?

deevus avatar Mar 13 '21 03:03 deevus