Wie schreibe ich eine TypeScript-Deklarationsdatei für ein komplexes externes commonjs-Modul mit Konstruktor, z. B. imap?

Diese Frage ist eine Verfeinerung meiner früheren:Wie schreibe ich eine TypeScript-Deklarationsdatei für ein externes commonjs-Modul mit Konstruktor?

Ich versuche, eine Deklarationsdatei für das imap-Modul zu schreiben: -imap auf npm - node-imap Wie soll diese Datei aussehen?

Nachdem ich oben keine zufriedenstellende Antwort auf meine ursprüngliche Frage gefunden habe, stelle ich fest, dass die Sprache einige konkurrierende Merkmale aufweist:

jeder Export oder Import macht ein Modul zu einem externen Moduls scheint keine Möglichkeit zu geben, mehrere Entitäten aus einem externen Modul mit einem Konstruktor auf Modulebene zu exportieren, sodass sich gemeinsam genutzte Entitäten außerhalb des Moduls (Umgebung) befinden müsse
Siehe meine vorherige Frage (oben).Es gibt keine Möglichkeit, externe Module in eine Deklarationsdatei außerhalb des deklarierten Moduls zu importieren.
Das folgende Muster ist nicht zulässig:import fs = require('fs'); declare module 'A' {}

Hier ist, was ich bisher für die Deklarationsdatei habe:

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;
}

Idealerweise werden alle Schnittstellen innerhalb des Moduls "imap" definiert, sodass der globale Geltungsbereich nicht verschmutzt wird.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage