美文网首页
为第三方库生成声明文件.d.ts

为第三方库生成声明文件.d.ts

作者: taiyosen | 来源:发表于2021-12-16 13:43 被阅读0次

    首先,你当然可以手写一个.d.ts,但这不是这里要讨论的,关于.d.ts该怎么写,参见文档:https://www.tslang.cn/docs/handbook/declaration-files/introduction.html

    比如你想在你的TypeScript项目中使用enquirer这个包,但是这个包没有提供.d.ts文件

    PS F:\tgit\smarttalk\tools\decide> npm i @types/enquirer -D
    npm ERR! code E404
    npm ERR! 404 Not Found - GET https://registry.npmjs.org/@types%2fenquirer - Not found
    npm ERR! 404  '@types/enquirer@*' is not in this registry.
    npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
    npm ERR! 404
    npm ERR! 404 Note that you can also install from a
    npm ERR! 404 tarball, folder, http url, or git url.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\fygame\AppData\Local\npm-cache\_logs\2021-12-16T03_44_43_085Z-debug-0.log
    

    我们可以使用现有的工具来生成声明文件,个人推荐使用dts-gen,其npm主页:
    https://www.npmjs.com/package/dts-gen

    全局安装
    npm i dts-gen -g

    根据其文档,你很快就知道该怎么用,我们就用dts-gen生成一个吧~

    先使用最简单的命令:dts-gen -m enquirer,但是你会遇到如下报错:

    PS F:\tgit\smarttalk\tools\decide> dts-gen -m enquirer
    Unexpected crash! Please log a bug with the commandline you specified.
    C:\Users\fygame\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\run.js:130
            throw e;
            ^
    
    TypeError: Cannot read property 'cursor' of undefined
        at EventEmitter.get cursor [as cursor] (F:\tgit\smarttalk\tools\decide\node_modules\enquirer\lib\prompt.js:398:23)
        at getProperty (C:\Users\fygame\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:254:30)
        at Array.map (<anonymous>)
        at getPropertyDeclarationsOfObject (C:\Users\fygame\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:250:25)
        at getTypeOfValue (C:\Users\fygame\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:237:33)
        at getProperty (C:\Users\fygame\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:257:45)
        at Array.map (<anonymous>)
        at getPropertyDeclarationsOfObject (C:\Users\fygame\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:250:25)
        at getTypeOfValue (C:\Users\fygame\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:237:33)
        at getResult (C:\Users\fygame\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\index.js:173:32)
    

    这类报错并不是总是会发生的,关键看你要生成声明文件的包的具体使用方式。详细看了下上述堆栈,你可以很快明白为什么会有上述错误产生。这里,dts-gen文档里关于--expression-file选项的使用就非常重要了。于是我们可以很简单地把enquirer的示例写在一个js文件中,如下:

    const { prompt } = require('enquirer');
     
    const response = await prompt({
      type: 'input',
      name: 'username',
      message: 'What is your username?'
    });
     
    console.log(response);
    

    然后我们用这个命令dts-gen --expression-file "index.js" -m enquirer,便可成功生成声明文件:

    PS F:\tgit\smarttalk\tools\decide> dts-gen --expresion-file "index.js" -m enquirer
    Wrote 8276 lines to enquirer.d.ts.
    

    生成的.d.ts文件片段如下:

    /** Declaration file generated by dts-gen */
    
    export = enquirer;
    
    declare class enquirer {
        constructor(...args: any[]);
    
        ask(...args: any[]): void;
    
        prompt(...args: any[]): void;
    
        register(...args: any[]): void;
    
        use(...args: any[]): void;
    
        static BasicAuth(...args: any[]): any;
    
        static Confirm(...args: any[]): void;
    
        static MultiSelect(...args: any[]): void;
    
        static addListener(p0: any, p1: any): any;
    
        static autocomplete(options: any): void;
    
        static basicauth(options: any): void;
    
        static captureRejectionSymbol: any;
    
        static captureRejections: boolean;
    
        static confirm(options: any): void;
    
        static defaultMaxListeners: number;
    
        static editable(options: any): void;
    ...
    

    相关文章

      网友评论

          本文标题:为第三方库生成声明文件.d.ts

          本文链接:https://www.haomeiwen.com/subject/wvhvfrtx.html