Search code examples
typescriptdefinitelytyped

Typescript: Using new in namespace


I'm working on creating DefinitelyTyped for the private package (I cannot change source code) and I cannot find any way to implement type like this:

  GlobalNameSpace.SuperClass = function(arg) {}
  GlobalNameSpace.superClass = new GlobalNameSpace.SuperClass(args)

My attempt:

declare namespace GlobalNameSpace {
    class SuperClass {}

    const superClass = new GlobalNameSpace.SuperClass(args);
}

Sadly when I do it I have error in VS Code.

A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.

Any idea how can I solve this?


Solution

  • Fixed it with the help from @elderapo on https://discord.gg/typescript

    Final solution:

    declare namespace GlobalNamespace {
        class SuperKomp {
            constructor();
            public on(key: string): void;
        }
    
        const komp: GlobalNamespace.SuperKomp;
    }
    
    GlobalNamespace.komp.on('click');