beyonk-group / svelte-google-analytics

Google Analytics component for Svelte
77 stars 12 forks source link

Convert to typescript #14

Closed kbsali closed 2 years ago

kbsali commented 2 years ago

Thanks a lot for this component!

I'm working on integrating it to my project and I was thinking it would be really improve the developer experience if it was implemented in typescript. We would benefit from IDE's autocompletion for instance rather than having to refer to generated-functions.js. Would you consider a PR for this? (not sure when I would have time to work on this though! :sweat_smile: )

colinhowells commented 2 years ago

Not sure if this is a good place to put this (maybe this should be in documentation), but if you want to use this without VSCode losing its mind you'll need this declaration in a global types (./src/global.d.ts, say) file:

declare module '@beyonk/svelte-google-analytics' {
    import { SvelteComponentTyped } from 'svelte';
    export class GoogleAnalytics extends SvelteComponentTyped<{ properties: string[] }> {}
    export const ga = {
        addEvent(event: string, data: object): void {},
        // repeat if needed for `all`, `ecommerce`, `retail`, `travel`, `games`, `setUserProperties` ...
        // refer to `svelte-google-analytics/src/generated-functions.js` after `npm run build`
        // but first install `@beyonk/async-script-loader` dependency to avoid build failure
    };
}
antony commented 2 years ago

I'm not going to implement this in TypeScript, but we will port it to SvelteKit which will generate types for you.