DEV Community 👩‍💻👨‍💻

C.OG
C.OG

Posted on • Updated on

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

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.

Top comments (3)

Collapse
smac89 profile image
smac89

No need to export global. It's called "global" for a reason

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.

🌚 Life is too short to browse without dark mode