0

Let's say we have following code:

interface SomethingLike {
  ping(): boolean;
}

declare namespace Api {
    class Something implements SomethingLike {
        constructor(descriptor: string);
        ping(): boolean;
    }   
}

In this particular case Something implements SomethingLike. Now let's rename SomethingLike to Something:

interface Something {
  ping(): boolean;
}

declare namespace Api {
    class Something implements Something {
        constructor(descriptor: string);
        ping(): boolean;
    }   
}

In this case class Something actually does not implements interface but rather implements ... itself in that sense that the heritage clause is resolved as class something (you can check this by invoking or just use go to definition in either Webstorm or Visual Studio Code).

The very fact that class in Typescript can implement itself surprises me a lot, however my question is - how can still I inherit from an eponymous classlike entity out of namespace?

shabunc
  • 23,119
  • 19
  • 77
  • 102
  • here's description of real-life case where I've encountered this https://github.com/DefinitelyTyped/DefinitelyTyped/issues/45532 – shabunc Jun 16 '20 at 23:00

1 Answers1

1

If the outer scope is itself in a namespace you can refer to it directly. If the outer scope is the global scope, then you can use the namespace globalThis as implemented in microsoft/TypeScript#29332:

declare namespace Api {
    class Something implements globalThis.Something {
        constructor(descriptor: string);
        ping(): boolean;
    }   
}

If you don't want to rely on globalThis you could also make a type alias for the shadowed type name:

type SomethingLike = Something;
declare namespace Api2 {
    class Something implements SomethingLike {
        constructor(descriptor: string);
        ping(): boolean;
    }
}

Okay, hope that helps; good luck!

Playground link to code

jcalz
  • 264,269
  • 27
  • 359
  • 360