DEV Community

loading...

TypeScript: How to set a new property on the window object

Colin Ogoo
Software Engineer, ❤️ Code
Updated on ・1 min read

When merging global interfaces in TypeScript, you have to declare the interface in the global scope.

export declare global {
  interface Window {
    // add you custom properties and methods
    YT:(videoID: string, options?: {}) => Observable<YtResponse>
  }
}

TypeScript supports declaration merging, the premise is the same for global objects. The difference is to declare the interface as part of the global scope.

Discussion (2)

Collapse
stevemu profile image
Steve Mu

It works great! Thank you

Collapse
chiranjib_b profile image
Chiranjib

This is super cool. Thank you for sharing.