错误TS2497:模块' ...'解析为非模块实体,无法使用此构造导入

时间:2017-07-07 18:26:23

标签: typescript types typescript-typings definitelytyped

鉴于此声明文件promise-retry/index.d.ts,遵循TypeScript的官方module-function.d.ts格式:

修改: 之前粘贴了错误的代码段(在下面仍然可见)

export = promiseRetry;

declare function promiseRetry(options: Partial<PromiseRetry.NodeRetryOptions>, fn: (retry: PromiseRetry.RetryFn, attempt: number) => Promise<any>): Promise<any>;
declare function promiseRetry(fn: (retry: PromiseRetry.RetryFn, attempt: number) => Promise<any>, options ?: Partial<PromiseRetry.NodeRetryOptions>): Promise<any>;

declare namespace PromiseRetry {
    export interface NodeRetryOptions {
        forever: boolean;
        unref: boolean;
        retries: number;
        factor: number;
        minTimeout: number;
        maxTimeout: number;
        randomize: number | false;
    }

    export type RetryFn = (err: any) => never;
}

我发现我无法通过以下语法导入NodeRetryOptionsRetryFn

import {NodeRetryOptions, RetryFn} from "promise-retry";

let options: NodeRetryOptions = { forever: false };
let retryFn: RetryFn = (err:any)=>{throw "Nobody responds on StackOverflow"};

...因为它抛出了编译错误:error TS2497: Module '"promise-retry"' resolves to a non-module entity and cannot be imported using this construct.

我还尝试了另一种语法:

declare module "promise-retry" {
    export = promiseRetry;

    function promiseRetry(options: Partial<PromiseRetry.NodeRetryOptions>, fn: (retry: PromiseRetry.RetryFn, attempt: number) => Promise<any>): Promise<any>;
    function promiseRetry(fn: (retry: PromiseRetry.RetryFn, attempt: number) => Promise<any>, options ?: Partial<PromiseRetry.NodeRetryOptions>): Promise<any>;

    namespace PromiseRetry {
        export interface NodeRetryOptions {
            forever: boolean;
            unref: boolean;
            retries: number;
            factor: number;
            minTimeout: number;
            maxTimeout: number;
            randomize: number | false;
        }

        export type RetryFn = (err: any) => never;
    }
}

但是进口(以不同的风格)也失败了:

import * as PromiseRetry from "promise-retry";

let options: PromiseRetry.NodeRetryOptions = { forever: false };
let retryFn: PromiseRetry.RetryFn = (err:any)=>{throw "Nobody responds on StackOverflow"};

...抛出相同的编译器错误。

我已经阅读了我能找到的所有内容(包括此处的相关链接),但我无法使其正常运行。用特朗普的话说,&#34;没有人知道TypeScript导入会如此复杂。&#34;

1 个答案:

答案 0 :(得分:1)

example you linked to在评论中有一个重要的注释:

/*~ Note that ES6 modules cannot directly export callable functions.
 *~ This file should be imported using the CommonJS-style:
 *~   import x = require('someLibrary');
 *~
 *~ Refer to the documentation to understand common
 *~ workarounds for this limitation of ES6 modules.
 */

您可能想要的是以下内容:

export = PromiseRetry;

declare function PromiseRetry(options: Partial<PromiseRetry.NodeRetryOptions>, fn: (retry: PromiseRetry.RetryFn, attempt: number) => Promise<any>): Promise<any>;
declare function PromiseRetry(fn: (retry: PromiseRetry.RetryFn, attempt: number) => Promise<any>, options ?: Partial<PromiseRetry.NodeRetryOptions>): Promise<any>;

declare namespace PromiseRetry {
    export interface NodeRetryOptions {
        forever: boolean;
        unref?: boolean;
        retries?: number;
        factor?: number;
        minTimeout?: number; // milliseconds
        maxTimeout?: number; // milliseconds|(Number.)Infinity, which is itself of number type.
        randomize?: number | false;
    }

    export type RetryFn = (err: any) => never;
}

请注意与命名空间匹配的大写PromiseRetry函数。然后,要导入,请使用以下行:

import PromiseRetry = require('promise-retry');

然后必须以命名空间为前缀:

let options: PromiseRetry.NodeRetryOptions = { forever: false };
let retryFn: PromiseRetry.RetryFn = (err:any) => {throw "Sometimes people respond on StackOverflow"};

作为旁注,似乎大多数软件包都倾向于使用名称空间将类型作为函数声明的名称空间子句附加。另一种允许您为函数和命名空间使用不同名称的替代方法如下:

export declare function promiseRetry(options: Partial<PromiseRetry.NodeRetryOptions>, fn: (retry: PromiseRetry.RetryFn, attempt: number) => Promise<any>): Promise<any>;
export declare function promiseRetry(fn: (retry: PromiseRetry.RetryFn, attempt: number) => Promise<any>, options ?: Partial<PromiseRetry.NodeRetryOptions>): Promise<any>;

declare namespace PromiseRetry {
    export interface NodeRetryOptions {
        forever: boolean;
        unref?: boolean;
        retries?: number;
        factor?: number;
        minTimeout?: number; // milliseconds
        maxTimeout?: number; // milliseconds|(Number.)Infinity, which is itself of number type.
        randomize?: number | false;
    }

    export type RetryFn = (err: any) => never;
}