1

I'm building a Nuxt project with TypeScript and need to access window.__NUXT__ because I want to load Algolia on SSR.

beforeMount() {
  const results =
    (this.$nuxt.context && this.$nuxt.context.nuxtState.algoliaState) ||
    window.__NUXT__.algoliaState

  this.instantsearch.hydrate(results)

  // Remove the SSR state so it can't be applied again by mistake
  delete this.$nuxt.context.nuxtState.algoliaState
  delete window.__NUXT__.algoliaState
},

TypeScript is complaining:

Property '__NUXT__' does not exist on type 'Window & typeof globalThis'.

Is there a way to tell TypeScript about __NUXT__?

J. Hesters
  • 13,117
  • 31
  • 133
  • 249

1 Answers1

0

You can augment the global.Window type with the __NUXT__ property:

  1. Create <projectRoot>/global.d.ts with these contents:

    // https://stackoverflow.com/a/59499895/6277151
    export {}
    
    declare global {
      interface Window {
        __NUXT__: any
      }
    }
    
  2. If using VS Code, restart the IDE to properly index the .d.ts file.

tony19
  • 125,647
  • 18
  • 229
  • 307