Search code examples
typescripttype-declarationcldr

Write TypeScript declaration file for callable module


The cldr-data package is defined something like:

function cldrData(path) {
  // ...
}

cldrData.all = function() {
  // ...
}

cldrData.entireMainFor = function(locale/*, ...*/) {
  // ...
}

cldrData.entireSupplemental = function() {
  // ...
}

module.exports = cldrData;

How should I write a type declaration file for such a package?


Solution

  • This seems to work:

    declare module 'cldr-data' {
        interface CldrData {
            (path: any, ...args: any[]): any;
            availableLocales: any;
            all(): any;
            entireMainFor(locale: any, ...args: any[]): any;
            entireSupplemental(): any;
        }
    
        declare const cldrData: CldrData;
        export = cldrData;
    };
    

    Usage:

    import cldrData from 'cldr-data';
    
    cldrData('main/en/numbers');
    cldrData.entireSupplemental();
    cldrData.entireMainFor('en');
    console.log(cldrData.all());