node.js - 如何为具有构造函数(例如 imap)的复杂外部 commonjs 模块编写 TypeScript 声明文件?

标签 node.js module typescript declaration

这个问题是我之前问题的改进:How do I write a TypeScript declaration file for an external commonjs module that has constructor?

我正在尝试为 imap 模块编写一个声明文件: -imap on npm -node-imap 这个文件应该是什么样子?

在没有找到上述原始问题的满意答案后,我意识到该语言存在一些相互竞争的功能:

  • 任何导出或导入都会使模块成为外部模块
  • 似乎无法从具有(模块级)构造函数的外部模块导出多个实体,因此共享实体必须位于模块外部(环境)。
    请参阅我之前的问题(见上文)。
  • 无法将外部模块导入到已声明模块之外的声明文件中
    不允许使用以下模式: 导入 fs = require('fs'); 声明模块“A”{}

这是迄今为止我所拥有的声明文件:

interface IIMAPAccount {
    user:           string;
    password:       string;
    host:           string;
    port:           number;
    tls?:           boolean;
}

interface IEMail {
    mail:            any;
}

interface ICriteria {
    // The following message flags are valid types that do not have arguments:
    ALL:            void;    // All messages.
    ANSWERED:       void;    // Messages with the Answered flag set.
    DELETED:        void;    // Messages with the Deleted flag set.
    DRAFT:          void;    // Messages with the Draft flag set.
    FLAGGED:        void;    // Messages with the Flagged flag set.
    NEW:            void;    // Messages that have the Recent flag set but not the Seen flag.
    SEEN:           void;    // Messages that have the Seen flag set.
    RECENT:         void;    // Messages that have the Recent flag set.
    OLD:            void;    // Messages that do not have the Recent flag set. This is functionally equivalent to "!RECENT" (as opposed to "!NEW").
    UNANSWERED:     void;    // Messages that do not have the Answered flag set.
    UNDELETED:      void;    // Messages that do not have the Deleted flag set.
    UNDRAFT:        void;    // Messages that do not have the Draft flag set.
    UNFLAGGED:      void;    // Messages that do not have the Flagged flag set.
    UNSEEN:         void;    // Messages that do not have the Seen flag set.

    // The following are valid types that require string value(s):

    BCC:            any;    // Messages that contain the specified string in the BCC field.
    CC:             any;    // Messages that contain the specified string in the CC field.
    FROM:           any;    // Messages that contain the specified string in the FROM field.
    SUBJECT:        any;    // Messages that contain the specified string in the SUBJECT field.
    TO:             any;    // Messages that contain the specified string in the TO field.
    BODY:           any;    // Messages that contain the specified string in the message body.
    TEXT:           any;    // Messages that contain the specified string in the header OR the message body.
    KEYWORD:        any;    // Messages with the specified keyword set.
    HEADER:         any;    // Requires two string values, with the first being the header name and the second being the value to search for. If this second string is empty, all messages that contain the given header name will be returned.
    // The following are valid types that require a string parseable by JavaScripts Date object OR a Date instance:
    BEFORE:         any;    // Messages whose internal date (disregarding time and timezone) is earlier than the specified date.
    ON:             any;    // Messages whose internal date (disregarding time and timezone) is within the specified date.
    SINCE:          any;    // Messages whose internal date (disregarding time and timezone) is within or later than the specified date.
    SENTBEFORE:     any;    // Messages whose Date header (disregarding time and timezone) is earlier than the specified date.
    SENTON:         any;    // Messages whose Date header (disregarding time and timezone) is within the specified date.
    SENTSINCE:      any;    // Messages whose Date header (disregarding time and timezone) is within or later than the specified date.
    //The following are valid types that require one Integer value:
    LARGER:         number;    // Messages with a size larger than the specified number of bytes.
    SMALLER:        number;    // Messages with a size smaller than the specified number of bytes.
    // The following are valid criterion that require one or more Integer values:
    UID:            any;    // Messages with UIDs corresponding to the specified UID set. Ranges are permitted (e.g. '2504:2507' or '*' or '2504:*').
}

interface IFetchOptions {
    markSeen:       boolean;  // Mark message(s) as read when fetched. Default: false
    struct:         boolean;  // Fetch the message structure. Default: false
    envelope:       boolean;  // Fetch the message envelope. Default: false
    size:           boolean;  // Fetch the RFC822 size. Default: false
    modifiers:      any;      // Fetch modifiers defined by IMAP extensions. Default: (none)
    bodies:         any;      // A string or Array of strings containing the body part section to fetch. Default: (none) Example sections:
}

declare module "imap" {
    import events                           = require('events');
    import EventEmitter                     = events.EventEmitter;

    interface IMAPFetch extends EventEmitter {
    }

    class IMAP extends EventEmitter {
        constructor(account : IIMAPAccount);
        connect();
        openBox(name : string, flag : boolean, cb : (err : Error, box) => void);
        search(criteria : ICriteria, cb : (error : Error, uids : string[]) => void);
        fetch(source : any, object : IFetchOptions) : IMAPFetch;
    }

    var out: typeof IMAP;
    export = out;
}

理想情况下,所有接口(interface)都将在模块“imap”内部定义,因此不会污染全局范围。

最佳答案

Ideally, all of the interfaces will be defined inside of module "imap", so there is no pollution of the global scope

而不是:

interface IFetchOptions {
    markSeen:       boolean;
    // so on
}
interface ICriteria {        
    ALL:            void;
}

执行以下操作

module imap {
    interface IFetchOptions {
        markSeen:       boolean;
        // so on
    }
    interface ICriteria {        
        ALL:            void;
    }
}

而且你只会用imap污染全局范围。这是一个非初始化模块,只能用于类型信息。

引用:查看幽灵模块:http://definitelytyped.org/guides/best-practices.html

关于node.js - 如何为具有构造函数(例如 imap)的复杂外部 commonjs 模块编写 TypeScript 声明文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26249210/

相关文章:

javascript - 解决失败: { Error: Cannot find module 'npm-watch'

node.js - 如何使用 Node.js 和 Axios 将文件上传到 AWS 中的预签名 URL?

javascript - 将 PHP Post API 脚本转换为 jQuery API 调用

c - C中两个整数的快速双向散列

c# - PowerShell Types.ps1xml 导入模块时找不到类型

typescript - Prisma FindMany 输入类型

typescript - 如何使用react-query替换context

typescript - 在 visual studio code 中,如何跳转到 typescript 类型定义 index.d.ts 中的实际代码?

javascript - Windows上的 Node npm错误寻找工具版本

module - 看不懂 Rust 模块系统