I'm trying to understand this GitHub post that describes extending a global interface locally.
However, since there is minimal commentary in the post, I'm struggling to understand what is going on. Below is the post I am referring to.
you can extend a global interface locally
declare global { interface String { myMegaMethod(): void; } } 'hey'.myMegaMethod(); // works
If I was to use the following pattern to extend the functionality of a native type like String
, how would I do so?
Do I just have a file called string.ts
containing the following snippet that I import whenever I want my Strings to have access to myMegaMethod
?
declare global {
interface String {
myMegaMethod(): void;
}
}