Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving user-defined class name conflict with built-in class name

I'm defining and implementing a Map class in TypeScript which also uses the built-in Map class (with generics). Obviously the names clash. Can I somehow import the built-in Map using explicit namespace (like one would do in Java) or is there a different way to resolve a conflict like this?

export class Map // my map
{
    private readonly items: Map<string, string>; // built-in map
}

I've looked through the TS core definition files but can't figure out if they use any namespace.

like image 282
Martynas Jusevičius Avatar asked Dec 20 '25 23:12

Martynas Jusevičius


1 Answers

I stumbled upon this question myself so I might as well provide a response after quite a bit of time wasted trying to find an answer myself.

In modern browsers, the global namespace can be accessed via the global variable 'globalThis' (e.g. globalThis.Map).

You'll need a polyfill to get this working in old browsers such as EdgeHTML.

like image 103
MoonStom Avatar answered Dec 24 '25 12:12

MoonStom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!