为导出带有属性的功能的模块定义类型

时间:2018-12-21 12:17:22

标签: typescript typescript-typings

对于打字稿来说是新手,我需要帮助来为第3方模块创建类型定义,该模块导出为具有附加功能作为属性的函数。

function myModule (options) { /* ... */ }
myModule.someMethod = function (options) { /* ... */ }
myModule.anotherMethod = function (options) { /* ... */}
module.exports = myModule;

到目前为止,通过查找如何定义具有我所介绍的属性的函数。但是“导出”有麻烦。

interface MyModuleOptions {
    someBoolean?: boolean;
}

export interface MyModule {
    (options?: MyModuleOptions): RegExp;
    someMethod(options?: MyModuleOptions): RegExp;
    anotherMethod(options?: MyModuleOptions): RegExp;
};

我在做什么错了?

更新

进行了更多的研究,最接近的是:

declare module "ip-regex" {
    function ipRegex(options?: ipRegex.IpRegexOptions): RegExp;

    namespace ipRegex {
        interface IpRegexOptions {
            exact?: boolean;
        }

        function v4(options?: IpRegexOptions): RegExp;
        function v6(options?: IpRegexOptions): RegExp;
    }
    export = ipRegex;
}

但是DefiniteltyType短毛绒抛出:

Error: /path/to/DefinitelyTyped/types/myModule/index.d.ts:6:1
ERROR: 6:1   no-single-declare-module    File has only 1 module declaration — write it as an external module. See: https://github.com/Microsoft/dtslint/blob/master/docs/no-single-declare-module.md
ERROR: 6:16  no-declare-current-package  Instead of declaring a module with `declare module "myModule"`, write its contents in directly in "index.d.ts". See: https://github.com/Microsoft/dtslint/blob/master/docs/no-declare-current-package.md

在不更改模块源代码的情况下是否可以修复?

1 个答案:

答案 0 :(得分:0)

最后只是跳过模块声明包装。

declare function ip(options?: ip.IpRegexOptions): RegExp;

declare namespace ip {
    interface IpRegexOptions {
        exact?: boolean;
    }

    function v4(options?: IpRegexOptions): RegExp;
    function v6(options?: IpRegexOptions): RegExp;
}
export = ip;