How to write a typescript definition file for a node module that exports a function?

前端 未结 2 1222
生来不讨喜
生来不讨喜 2021-01-11 23:59

Consider, for the toml node module I can simply use:

// toml.d.ts
declare module TOML {
    export function parse(value:string):any;
}

declare module \"toml         


        
相关标签:
2条回答
  • 2021-01-12 00:16

    Use export =.

    Definition:

    declare module 'glob' {
      function globs(paths: string, options: any, callback: (err: any, files: string[]) => void): any;
      export = globs;
    }
    

    Usage (with esModuleInterop enabled):

    import glob from 'glob';
    glob("*.js", {}, (err, files) => { });
    
    0 讨论(0)
  • 2021-01-12 00:18

    Basarat's answer doesn't work with typescript 2.1.5. You need to declare function and export with export =:

    export = MyFunction;
    declare function MyFunction(): string;
    

    How to write a definition file for commonjs module that exports function

    0 讨论(0)
提交回复
热议问题