DEV Community

C.OG
C.OG

Posted on • Edited on

18 4

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
Qi Mu

It works great! Thank you

Collapse
 
chiranjib_b profile image
Chiranjib

This is super cool. Thank you for sharing.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay