Search code examples
typescriptdefinitelytyped

TypeScript: Subfunction in DefinitelyTyped


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.foo = function(arg) {} 
  GlobalNameSpace.foo.bar1 = function(arg) {} ;
  GlobalNameSpace.foo.foo2 = function(arg) {};

I've tried using class in my index.d.ts but foo is is not a class sadly is a normal function.

my attempt:

declare namespace GlobalNameSpace {
    function foo(options: any): void;
}

Any idea how can I solve this?


Solution

  • Any function, such as:

    function foo(options: any): void;
    

    can be expressed as a callable object, like so:

    const foo: {
        (options: any): void;
    }
    

    Also, any object can have additional properties. These properties can be callable as well, so the following is perfectly valid:

    declare namespace GlobalNameSpace {
        const foo: {
            (options: any): void;
    
            bar1: { (arg: any): void };
            foo2: { (arg: any): void };
        }
    }
    

    Note that a const variable is slightly different from a function. Functions are hoisted for example. But in the case above they're equivalent.