Can someone explain the concept of extending a global interface locally as mentioned in this GitHub post?
I find it difficult to grasp the idea due to lack of explanation in the post. The post in question is as follows.
you can extend a global interface locally
declare global { interface String { myMegaMethod(): void; } } 'hey'.myMegaMethod(); // works
If I want to enhance the functionality of a built-in type such as String
using this method, how would I go about it?
Would creating a file named string.ts
with the following code snippet and importing it whenever needed provide access to myMegaMethod
for Strings?
declare global {
interface String {
myMegaMethod(): void;
}
}