gtm-module icon indicating copy to clipboard operation
gtm-module copied to clipboard

Typescript

Open acamenhas opened this issue 4 years ago • 8 comments

Hi all,

In my nuxt TS project i cant access the $gtm variable. Do i need to declare it in index.d.ts? If so, can anybody give me an hint to make it happen in TS?

Thank you

acamenhas avatar May 12 '20 10:05 acamenhas

Hey ! That's how we fixed it : nuxt.d.ts

declare module "vue/types/vue" {
  interface Vue {
    $gtm: {
      pushEvent(event: { event: DatalayerEvents; [key: string]: any }): void;
    };
  }
}

MFrizzy avatar May 16 '20 14:05 MFrizzy

Yoo!!

Thank you it worked!

acamenhas avatar May 18 '20 18:05 acamenhas

Hello @MFrizzy . I have the same problem but when I use your code I receive the following error: "Cannot find name 'DatalayerEvents'"

igor-pereira avatar May 19 '20 08:05 igor-pereira

Hey @igor-pereira That's a custom type i've made with my GTM Events : type DatalayerEvents = "login" | "register"; You can use an enumeration if you want ;)

MFrizzy avatar May 19 '20 09:05 MFrizzy

Hey @MFrizzy I managed to make it work using:

declare module "vue/types/vue" { interface Vue { $gtm: { push(event: { event: string; [key: string]: any }): void; }; } }

igor-pereira avatar May 19 '20 09:05 igor-pereira

Doing this hack works for $gtm, but then other types don't work anymore.

this.$store this.$root this.$i18n ...

Hey @MFrizzy I managed to make it work using:

declare module "vue/types/vue" { interface Vue { $gtm: { push(event: { event: string; [key: string]: any }): void; }; } }

leonardlin avatar Oct 07 '21 12:10 leonardlin

Doing this hack works for $gtm, but then other types don't work anymore.

this.$store this.$root this.$i18n ...

Hey @MFrizzy I managed to make it work using: declare module "vue/types/vue" { interface Vue { $gtm: { push(event: { event: string; [key: string]: any }): void; }; } }

Had the same problem, make sure to import vue in the .d.ts file first, otherwise you're overriding the Vue interface instead of augmenting it.

Like this:

import 'vue'

declare module 'vue/types/vue' {
    interface Vue {
        $gtm: {
            push(event: { event: string; [key: string]: any }): void;
        };
    }
}

derfl007 avatar Dec 04 '21 18:12 derfl007

I was facing the same issue, I needed to use $gtm on custom plugins so I also added the typing there. Notice that I am using event? because sometimes I don't need to pass it but I disagree with this approach so if you always pass event you can take out the ?.

I followed Nuxt TypeScript Docs.

import 'vue'

declare module "vue/types/vue" {
  // this.$gtm inside Vue components
  interface Vue {
    $gtm: {
      push(event: {event?: string, [p: string]: any}): void
    };
  }
}

declare module '@nuxt/types' {
  // nuxtContext.app.$gtm inside asyncData, fetch, plugins, middleware, nuxtServerInit
  interface Context {
    $gtm: {
      push(event: {event?: string, [p: string]: any}): void
    };
  }
}

declare module 'vuex/types/index' {
  // this.$gtm inside Vuex stores
// eslint-disable-next-line @typescript-eslint/no-unused-vars,unused-imports/no-unused-vars
  interface Store<S> {
    $gtm: {
      push(event: {event?: string, [p: string]: any}): void
    };
  }
}```

ryansegus avatar May 12 '23 11:05 ryansegus