Nono.MA

Extend the window interface in TypeScript, React, Vite & Next

FEBRUARY 8, 2024

You can directly assign new properties.

(window as any).talk = () => { console.log(`Hello!`) }

You can extend the Window interface with typings and then assign the property values.

declare global {
  interface Window {
    talk: () => void
    concat: (words: string[]) => string
  }
}

window.talk = () => { console.log(`Hello!`) }
window.concat = (words: string[]) => {
  return words.join(`, `)
}

BlogTypescriptReactVite