typescriptglobal-variablesumd.d.tstypescript-definitions

How to declare an UMD module which only expose one function?


Here's what I currently have:

export default function (bar?: any): Foo

export as namespace qux;

Assuming we have no module loader, what should I change so that qux may be called directly? What I am trying to avoid is: qux.default()
In short I want the global variable to match the default export.

(of course import defaultName from 'qux'; defaultName() should continue to work)


Solution

  • Exportation

    export = qux;
    export as namespace qux;
    
    declare function qux(bar?: any): Foo
    
    interface Foo {}
    

    Importation

    .ts file

    import defaultName = require('qux');
    

    .js file

    import defaultName from 'qux';