Search code examples
javascripttypescriptinheritanceprototypeprototypal-inheritance

How to explicitly identify entities from global namespace in Typescript?


Instead of using Javascript's prototype, I am thinking of using inheritance feature of Typescript.

I want to add few useful methods in the class String, which belongs to my namespace Util. So this String implements the global interface String.
How to differentiate between these two? e.g. in C++ we do something like:

namespace Util
{
  class String : public ::String { ...

What is the Typescript equivalent for :: to inform compiler that the interface belongs to the global space?


Add-on Qn: Is the class inheritance the right alternative for Javascript prorotype?
What is the alternative equivalent of Javascript "prototype" in Typescript?


Solution

  • What is the Typescript equivalent for :: to inform compiler that the interface belongs to the global space?

    There is no keyword for it and the request was declined : https://github.com/Microsoft/TypeScript/issues/983

    You can create a global reference to avoid name collision

    type GlobalString = String; 
    namespace Util {
      // Use GlobalString
    }